jcunews

DOM Snapshot bookmarklet

Feb 16th, 2022 (edited)
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript: /*DOMSnapshot*/ (rl => {
  2.   function htm(t, m, a, x) {
  3.     if(document.head && !document.head.querySelector("base")) {
  4.       (x = document.createElement("BASE")).href = location.href.split("?")[0];
  5.       document.head.appendChild(x)
  6.     } else x = null;
  7.     t = true;
  8.     m = Array.from(document.childNodes).map((nd, i) => {
  9.       switch (nd.nodeType) {
  10.         case Node.COMMENT_NODE:
  11.         case Node.TEXT_NODE:
  12.           return `<!--${nd.data}-->\n`;
  13.         case Node.ELEMENT_NODE:
  14.           return nd.outerHTML + "\n";
  15.         case Node.DOCUMENT_TYPE_NODE:
  16.           t = !(nd.publicId || nd.systemId);
  17.           return `<!${(/\bxml\b/).test(document.contentType) ? "DOCTYPE" : "doctype"} ${nd.name}${
  18.             nd.publicId ? ` PUBLIC "${nd.publicId}"` : ""
  19.           }${nd.systemId ? ` "${nd.systemId}"` : ""}>\n`;
  20.         default:
  21.           console.log("Unprocessed node", nd);
  22.           return `<!--(NodeType${nd.nodeType})-->\n`
  23.       }
  24.     });
  25.     if (document instanceof XMLDocument) m.unshift(
  26.       `<?xml version="1.0" encoding="${document.characterSet}" standalone="${t ? "yes" : "no"}"?>\n`
  27.     );
  28.     rl.forEach(r => {
  29.       r[1].parentNode.insertBefore(r[0], r[1]);
  30.       r[1].parentNode.removeChild(r[1])
  31.     });
  32.     a = document.createElement("A");
  33.     a.href = URL.createObjectURL(new Blob([
  34.       '<!doctype html>\n<body style="white-space:pre;font-family:monospace">\n' + m.join("").replace(/</g, "&lt;") + '</body>'
  35.     ], {type: "text/html"}));
  36.     if(x) x.parentNode.removeChild(x);
  37.     if(t = document.title) {
  38.       x = {
  39.         "\"": "\u201c",
  40.         "*": "\u20f0",
  41.         "/": "-",
  42.         ":": "\ua789",
  43.         "<": "\u2264",
  44.         ">": "\u2265",
  45.         "?": "\xbf",
  46.         "\\": "-",
  47.         "|": "\u2502"
  48.       };
  49.       t = t.replace(/["*\/:<>?\\|]/g, c => x[c]) + " - "
  50.     }
  51.     m = (m = location.pathname.match(/\/([^\/]+)$/)) ? m[1] : "";
  52.     a.target = "_blank";
  53.     if(a.click) {
  54.       a.click()
  55.     } else open(a.href, "_blank")
  56.   }
  57.   function subsEle(ri, u, h, e) {
  58.     e = document.createElement(rl[ri].href ? "style" : "script");
  59.     e.setAttribute("source", u);
  60.     e.innerHTML = h;
  61.     rl[ri] = [rl[ri], e];
  62.     rl[ri][0].parentNode.insertBefore(e, rl[ri][0]);
  63.     rl[ri][0].parentNode.removeChild(rl[ri][0]);
  64.     if(++ri < rl.length) {
  65.       getRes(ri)
  66.     } else htm()
  67.   }
  68.   function getRes(ri, u) {
  69.     u = rl[ri].href || rl[ri].src;
  70.     console.log("Fetching: " + u);
  71.     fetch(u).catch((er, e) => {
  72.       console.log("Error fetching: " + u, er);
  73.       alert(`Error fetching:\n${u}\n\n${er}`);
  74.       subsEle(ri, u, `/* ${er} */`)
  75.     }).then(r => r.text()).then((t, e) => {
  76.       console.log("Fetched: " + u);
  77.       subsEle(ri, u, t)
  78.     });
  79.   }
  80.   rl = Array.from(document.querySelectorAll("link[href][rel=stylesheet],script[src]")).filter(e => e.href || e.src);
  81.   if(rl.length) {
  82.     getRes(0)
  83.   } else htm()
  84. })()
  85.  
Add Comment
Please, Sign In to add comment