Skip to content

Instantly share code, notes, and snippets.

@z010107
Created August 31, 2018 08:13
Show Gist options
  • Save z010107/acb01dbf452f9a3037118951848e1d99 to your computer and use it in GitHub Desktop.
Save z010107/acb01dbf452f9a3037118951848e1d99 to your computer and use it in GitHub Desktop.
Grab page source with chrome extension
function DOMtoString(document_root) {
var html = '',
node = document_root.firstChild;
while (node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
html += node.outerHTML;
break;
case Node.TEXT_NODE:
html += node.nodeValue;
break;
case Node.CDATA_SECTION_NODE:
html += '<![CDATA[' + node.nodeValue + ']]>';
break;
case Node.COMMENT_NODE:
html += '<!--' + node.nodeValue + '-->';
break;
case Node.DOCUMENT_TYPE_NODE:
// (X)HTML documents are identified by public identifiers
html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
break;
}
node = node.nextSibling;
}
return html;
}
chrome.runtime.sendMessage({
action: "getSource",
source: DOMtoString(document)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment