Skip to content

Instantly share code, notes, and snippets.

@bt4R9
Last active August 29, 2015 14:25
Show Gist options
  • Save bt4R9/2a969a8a928133085346 to your computer and use it in GitHub Desktop.
Save bt4R9/2a969a8a928133085346 to your computer and use it in GitHub Desktop.
function parseDOM() {
var root = document.querySelector('[data-key]').cloneNode(true);
var notNsNodes = root.querySelectorAll('*:not([data-key^=view]):not([data-key^=box])');
[].forEach.call(notNsNodes, function(node) {
node.parentNode.removeChild(node);
});
function getJSON() {
function getObjectNode(node) {
var jsonNode = {};
if (node.nodeType === 1) {
var children = (node.childNodes = [].map.call(node.childNodes, getObjectNode));
var key = node.getAttribute('data-key');
var isBox = key.indexOf('box') !== -1;
var isVisible = node.className.indexOf('ns-view-visible') !== -1;
var view = key.split('&')[0].split('=')[1];
var params = {};
var paramsIndex = key.indexOf('&');
if (paramsIndex !== -1) {
var paramsKey = key.slice(paramsIndex + 1);
paramsKey.split('&').forEach(function(key) {
var _params = key.split('=');
params[_params[0]] = _params[1];
})
}
jsonNode = {
view: view,
params: params,
isBox: isBox,
isVisible: isVisible
};
if (children.length) {
jsonNode.children = children;
}
}
return jsonNode;
}
return getObjectNode(root);
}
var json = getJSON();
chrome.runtime.sendMessage({type: "dom", json: json});
console.log('extenstion', json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment