From 35d82bd801146ce4f37be1e5eb798e6840ff6f3d Mon Sep 17 00:00:00 2001 From: hackademix Date: Wed, 29 Jul 2020 00:42:20 +0200 Subject: [PATCH] More accurate NOSCRIPT element emulation. --- src/content/content.js | 1 + src/content/onScriptDisabled.js | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/content/content.js b/src/content/content.js index 62d3cf9..3bb8359 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -102,6 +102,7 @@ window.addEventListener("securitypolicyviolation", e => { Messages.send("violation", {url, type}); }, true); + ns.on("capabilities", () => { seen.record({ request: { diff --git a/src/content/onScriptDisabled.js b/src/content/onScriptDisabled.js index 4727eb2..d0ebfd4 100644 --- a/src/content/onScriptDisabled.js +++ b/src/content/onScriptDisabled.js @@ -6,26 +6,36 @@ function onScriptDisabled() { onScriptDisabled = () => {}; let refresh = false; for (let noscript of document.querySelectorAll("noscript")) { + // force show NOSCRIPT elements content - let replacement = createHTMLElement("span"); - replacement.innerHTML = noscript.innerHTML; - noscript.replaceWith(replacement); + let replacement = document.createRange().createContextualFragment(noscript.innerHTML); // emulate meta-refresh - let meta = replacement.querySelector('meta[http-equiv="refresh"]'); - if (meta) { + for (let meta of replacement.querySelectorAll('meta[http-equiv="refresh"]')) { refresh = true; document.head.appendChild(meta); + console.log(`State %s, emulating`, document.readyState, meta); } + + if (noscript.closest("head") && document.body) { + document.body.insertBefore(noscript, document.body.firstChild); + } + noscript.replaceWith(replacement); } if (refresh) { let html = document.documentElement.outerHTML; - window.addEventListener("load", e => { - if (!e.isTrusted) return; + let rewrite = () => { let document = window.wrappedJSObject ? window.wrappedJSObject.document : window.document; document.open(); document.write(html); document.close(); - }); + }; + if (document.readyState === "complete") { + rewrite(); + } else { + window.addEventListener("load", e => { + if (e.isTrusted) rewrite(); + }); + } } { let eraser = {