getPagesSource.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // @author Rob W <http://stackoverflow.com/users/938089/rob-w>
  2. // Demo: var serialized_html = DOMtoString(document);
  3. function DOMtoString(document_root) {
  4. var html = '',
  5. node = document_root.firstChild;
  6. while (node) {
  7. switch (node.nodeType) {
  8. case Node.ELEMENT_NODE:
  9. html += node.outerHTML;
  10. break;
  11. case Node.TEXT_NODE:
  12. html += node.nodeValue;
  13. break;
  14. case Node.CDATA_SECTION_NODE:
  15. html += '<![CDATA[' + node.nodeValue + ']]>';
  16. break;
  17. case Node.COMMENT_NODE:
  18. html += '<!--' + node.nodeValue + '-->';
  19. break;
  20. case Node.DOCUMENT_TYPE_NODE:
  21. // (X)HTML documents are identified by public identifiers
  22. html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
  23. break;
  24. }
  25. node = node.nextSibling;
  26. }
  27. return html;
  28. }
  29. //window.onload = function() {
  30. setTimeout(function () {
  31. chrome.runtime.sendMessage({
  32. action: "getSource",
  33. source: DOMtoString(document)
  34. });
  35. }, 600);
  36. //};