dom-data
v0.2.0
Extract data from the DOM, ready to be used in your JavaScript.
Designed to tackle the SEO problem faced by SPA's: search engines only see an empty page if you get your data from XHR or WebSockets. With this module, you can send your data to the client in the HTML document - probably hidden from view, but still visible to search engines. Just place the bundle.js
file in a script tag, extract the data as shown below, and present it in a nicer way to your users.
API
DomData(string|element root, Object template) -> Object
Extracts data from root
, following the rules in template
and returns the result.
To explain the rules, an example is probably the most effective, so here you go:
page.html:
... ... This is the title of the first article This is the intro to the article. It's not too long This is the first line of the article Here is another line And this is the last line {"first": "Tuur", "last": "Dutoit"} Wed Jul 27 2016 14:19:06 GMT+0200 (CEST) true 243 Second article Third article ... ... ...
extract.js:
var template = // $$: select an element $$: "article" // attribute(): select an attribute of the root element id: DomData // text(): optional, it's the default getter title: DomDatatext // filter(): add a (custom) filter; inline, in this case. Pass a string to add a registered filter. intro: DomData // Extract the inner HTML content: DomData // Convert to a Date object posted: DomData // Convert to a number numberOfReads: DomData // Filters can be chained subheader: DomData // Check if an attribute exists (works for properties too) active: DomData // Alternatively: active: DomData // Nested objects are allowed and will be automatically traversed, // mirroring their structure in the result author: name: DomData age: DomData // Array: Adds an entry for every element that matches the query related: DomData ; var canvas = document;var data = ;
data:
id: "344147334" title: "This is the title of the first article" intro: "This is the intro to" content: "<p>This is the first line of the article</p>\n<p>Here is another line</p>\n<p>And this is the last line</p>" posted: Date"Wed Jul 27 2016 14:19:06 GMT+0200 (DEST)" numberOfReads: 243 subheader: "1243 people have read this article" author: first: "Tuur" last: "Dutoit" related: "785645677" "235645675" "...": "..."
License
The MIT License (MIT) Copyright (c) 2016 Tuur Dutoit
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.