Advertisement
jcunews

DOM Snapshot Download bookmarklet

Feb 16th, 2022
511
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript: /*DOMSnapshotDownload*/ (rl => {
  2.   function htm(t, m, a, c, 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.     c = document.contentType;
  9.     m = Array.from(document.childNodes).map((nd, i) => {
  10.       switch (nd.nodeType) {
  11.         case Node.COMMENT_NODE:
  12.         case Node.TEXT_NODE:
  13.           return `<!--${nd.data}-->\n`;
  14.         case Node.ELEMENT_NODE:
  15.           return nd.outerHTML + "\n";
  16.         case Node.DOCUMENT_TYPE_NODE:
  17.           t = !(nd.publicId || nd.systemId);
  18.           return `<!${(/\bxml\b/).test(c) ? "DOCTYPE" : "doctype"} ${nd.name}${
  19.             nd.publicId ? ` PUBLIC "${nd.publicId}"` : ""
  20.           }${nd.systemId ? ` "${nd.systemId}"` : ""}>\n`;
  21.         default:
  22.           console.log("Unprocessed node", nd);
  23.           return `<!--(NodeType${nd.nodeType})-->\n`
  24.       }
  25.     });
  26.     if(document instanceof XMLDocument) {
  27.       m.unshift(`<?xml version="1.0" encoding="${document.characterSet}" standalone="${t ? "yes" : "no"}"?>\n`)
  28.     }
  29.     rl.forEach(r => {
  30.       r[1].parentNode.insertBefore(r[0], r[1]);
  31.       r[1].parentNode.removeChild(r[1])
  32.     });
  33.     a = document.createElement("A");
  34.     a.href = URL.createObjectURL(new Blob(m, {type: c}));
  35.     if(!c.includes("xml")) {
  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.       if (!(m = (m = location.pathname.match(/\/([^\/]+)$/)) ? m[1] : "")) t = t.replace(/ - $/, "");
  52.       a.download = t + ((/\.html?$/i).test(m)
  53.         ? m
  54.         : m + "." + ([["xhtml", "xhtml"], ["html", "html"], ["xml", "xml"], ["text", "txt"]].reduce((r, k) => {
  55.           if(!r && c.includes(k[0])) r = k[1];
  56.           return r
  57.         }, "") || "bin"));
  58.       a.target = "_blank";
  59.       a.click()
  60.     } else open(a.href, "_blank")
  61.   }
  62.    function subsEle(ri, u, h, e) {
  63.     e = document.createElement(rl[ri].href ? "style" : "script");
  64.     e.setAttribute("source", u);
  65.     e.innerHTML = h;
  66.     rl[ri] = [rl[ri], e];
  67.     rl[ri][0].parentNode.insertBefore(e, rl[ri][0]);
  68.     rl[ri][0].parentNode.removeChild(rl[ri][0]);
  69.     if(++ri < rl.length) {
  70.       getRes(ri)
  71.     } else htm()
  72.   }
  73.   function getRes(ri, u) {
  74.     u = rl[ri].href || rl[ri].src;
  75.     console.log("Fetching: " + u);
  76.     fetch(u).catch((er, e) => {
  77.       console.log("Error fetching: " + u, er);
  78.       alert(`Error fetching:\n${u}\n\n${er}`);
  79.       subsEle(ri, u, `/* ${er} */`)
  80.     }).then(r => r.text()).then((t, e) => {
  81.       console.log("Fetched: " + u);
  82.       subsEle(ri, u, t)
  83.     })
  84.   }
  85.   rl = Array.from(document.querySelectorAll("link[href][rel=stylesheet],script[src]")).filter(e => e.href || e.src);
  86.   if(rl.length) {
  87.     getRes(0)
  88.   } else htm()
  89. })()
  90.  
Advertisement
RAW Paste Data Copied
Advertisement