Skip to content

Instantly share code, notes, and snippets.

@softwarespot
Created July 1, 2017 21:32
Show Gist options
  • Save softwarespot/9ee80dc82b474998ff1b4ad717f227a1 to your computer and use it in GitHub Desktop.
Save softwarespot/9ee80dc82b474998ff1b4ad717f227a1 to your computer and use it in GitHub Desktop.
// URL: https://github.com/wildpeaks/package-snapshot-dom/blob/master/src/snapshot.js
console.clear();
function snapshotDOM(node, nodes, path) {
nodes = nodes || [];
if (!isObject(node)) {
return nodes;
}
var snapshot = {};
snapshot.node = node.cloneNode(true);
snapshot.path = (path ? path + '.' : '') + node.nodeName.toLowerCase();
const childNodes = Array.from(node.childNodes);
if (childNodes.length) {
childNodes.forEach(childNode => snapshotDOM(childNode, nodes, snapshot.path));
}
nodes.push(snapshot);
return nodes;
}
function isObject(obj) {
return Object(obj) === obj;
}
console.log(snapshotDOM(document.body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment