Last active
August 29, 2015 13:58
-
-
Save philmander/10012466 to your computer and use it in GitHub Desktop.
Microdata util for CasperJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//microdata polyfill (see https://github.com/termi/Microdata-JS) | |
casper.options.clientScripts.push("../../../support/a.js"); | |
casper.options.clientScripts.push("../../../support/microdata-js.js"); | |
/** | |
* Fetches microdata from the remote DOM environment, in a json object structure. | |
* @param itemType The item type of item scopes to fetch data for | |
* @returns {Object|mixed} | |
*/ | |
casper.fetchMicrodata = function(itemType) { | |
"use strict"; | |
return casper.evaluate(function(itemType) { | |
//this function is adapted from https://github.com/abernier/microdatajs/blob/master/jquery.microdata.json.js | |
var getMicrodata = function(itemType) { | |
function getObject(item, memory) { | |
var result = {}; | |
result.type = item.itemType; | |
result.id = item.itemId; | |
result.properties = {}; | |
[].slice.call(item.properties).forEach(function (elem) { | |
var value; | |
if (elem.itemScope) { | |
if (memory.indexOf(elem) > -1) { | |
memory.push(item); | |
value = getObject(elem, memory); | |
memory.pop(); | |
} | |
} else { | |
value = elem.itemValue; | |
} | |
[].slice.call(elem.itemProp).forEach(function (prop) { | |
result.properties[prop] = result.properties[prop] || []; | |
result.properties[prop].push(value); | |
}); | |
}); | |
return result; | |
} | |
var result = {}; | |
result.items = []; | |
var items = document.getItems(itemType); | |
[].slice.call(items).forEach(function (item) { | |
result.items.push(getObject(item, [])); | |
}); | |
return result; | |
}; | |
return getMicrodata(itemType); | |
}, itemType); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment