Skip to content

Instantly share code, notes, and snippets.

@foxinni
Created August 21, 2012 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxinni/3413475 to your computer and use it in GitHub Desktop.
Save foxinni/3413475 to your computer and use it in GitHub Desktop.
Javascript from the Schema Explorer Chrome Extension
/*
Content Script
*/
(
function (document, window)
{
var __dataTree = [];
var __renderFlag = 0;
var __parse = function()
{
__explore(document.body);
}
var __render = function()
{
if (__dataTree.length && !__renderFlag)
{
__renderFlag++;
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.bottom = '0px';
div.style.right = '0px';
div.style.width = '100%';
div.style.height = '300px';
div.style.borderTop = '3px solid #ddd';
div.style.overflowY = 'scroll';
div.style.textAlign = 'left';
div.style.backgroundColor = 'rgba(250, 250, 250, 0.9)';
div.style.color = '#222';
div.innerHTML = '<div style="padding-left : 20px; padding-right : 20px">' + __addData(__dataTree) + '</div>';
div.style.padding = '10px';
div.style.display = 'block';
div.style.zIndex = 1000000;
div.id = '__schema__EXT';
document.body.appendChild(div);
// placeholder to allow scroll
var div = document.createElement('div');
div.style.height = '330px';
div.innerHTML = '&nbsp;';
div.id = '__schema__EXT_PH';
document.body.appendChild(div);
}
else if (__renderFlag)
{
/*
if (__renderFlag == 1)
{
__renderFlag = 2;
document.getElementById('__schema__EXT').style.display = 'none';
document.getElementById('__schema__EXT_PH').style.display = 'none';
}
else
{
__renderFlag = 1;
document.getElementById('__schema__EXT').style.display = 'block';
document.getElementById('__schema__EXT_PH').style.display = 'block';
}
*/
}
}
var __addData = function (data)
{
var str = '';
var i = 0;
while (i < data.length)
{
var l = data[i++];
if (l.name == 'scope')
{
str +=
"<div style='padding-left : 10px; overflow : hidden; height : 17px' ><div onclick='if (this.parentNode.style.height != \"auto\"){this.parentNode.style.height = \"auto\";}else{this.parentNode.style.height = \"15px\";}'><b> [+]" + ((l.value) ? l.value : '') + '(<a href="' + l.type + '">' + l.type +'</a>)' + "</b></div>";
var j = 0;
while (j < l.childs.length)
{
str += __addData(l.childs[j]);
++j;
}
str += '</div>';
}
else
{
str +=
"<div><i>" + l.name + "</i> : " + l.value + "</div>";
}
}
return str;
}
var __explore = function(node, parentData)
{
if (parentData === null || parentData === undefined)
{
parentData = __dataTree;
}
if (node.getAttribute)
{
var isItemScope = node.getAttribute('itemscope');
var hasItemProp = node.getAttribute('itemprop');
var itemtype = node.getAttribute('itemtype');
var childs = node.childNodes;
var i = 0;
var tmp = new Array();
while (i < childs.length)
{
if (isItemScope !== null)
__explore(childs[i], tmp);
else
__explore(childs[i], null);
++i;
}
if (isItemScope !== null)
{
parentData.push({name : 'scope', value : hasItemProp, type : itemtype, childs : [tmp], node : node});
}
else if (hasItemProp && parentData)
{
parentData.push({name : hasItemProp, value : node.innerText});
}
}
}
var regex = /itemprop/;
// Test the text of the body element against our regular expression.
if (regex.test(document.body.innerHTML))
{
// The regular expression produced a match, so notify the background page.
chrome.extension.sendRequest({}, function(response) {});
__parse();
window.__render__THE__Matrix = __render;
} else {
// No match was found.
}
}
)(document, window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment