Skip to content

Instantly share code, notes, and snippets.



Last active Jul 23, 2020
What would you like to do? microdata parser in Javascript for the client side.
Copied from
without jQuery.
var Schema = (function(doc){
function getTypes(node){
var type = node.getAttribute('itemtype');
if(!type) return [];
return type.split(' ');
function getValue(node){
if(node.getAttribute('itemprop') === null) return null;
case 'meta':
return node.getAttribute('content') || '';
case 'audio':
case 'embed':
case 'iframe':
case 'img':
case 'source':
case 'track':
case 'video':
return node.getAttribute('src');
case 'a':
case 'area':
case 'link':
return node.getAttribute('href');
case 'object':
return node.getAttribute('data');
case 'data':
return node.getAttribute('value') || '';
case 'time':
return node.getAttribute('datetime');
return node.innerHTML;
return {
toObject: function(node, memory){
var result = {properties: {}};
result.type = getTypes(node);
var itemid = node.getAttribute('itemid');
if(itemid) = itemid;
var properties = node.querySelectorAll('[itemprop]');
for(var i = 0; i < properties.length; i++){
var value = null;
var item = properties[i];
var key = item.getAttribute('itemprop');
if(item.getAttribute('itemscope') !== null){
if(memory.indexOf(item) !== -1){
value = 'ERROR';
value = this.toObject(item, memory);
value = getValue(item);
}[key] = value;
return result;
, scopes: function(nodes){
if(!nodes) nodes = doc.querySelectorAll('[itemscope]:not([itemprop])');
var scopes = [];
for(var i = 0; i < nodes.length; i++){
var scope = nodes[i];
scopes.push(this.toObject(scope, []));
return scopes;
win.Schema = Schema;

This comment has been minimized.

Copy link

@futuretap futuretap commented Jul 23, 2020

Doesn't work with nested scopes:

<html itemscope itemtype="" class="no-js" lang="nl">
    <meta itemprop="name" content="Some article name">
    <div itemscope itemtype="">
      <span itemprop="name">Skyfall</span>

The article name is detected as Skyfall, not Some article name.


This comment has been minimized.

Copy link
Owner Author

@joeyguerra joeyguerra commented Jul 23, 2020

Yeah, good catch. Would probably require a different design for the scopes function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment