diff options
Diffstat (limited to 'content/main.js')
-rw-r--r-- | content/main.js | 99 |
1 files changed, 17 insertions, 82 deletions
diff --git a/content/main.js b/content/main.js index 164ebe3..441636c 100644 --- a/content/main.js +++ b/content/main.js @@ -11,87 +11,24 @@ * IMPORT handle_page_actions * IMPORT extract_signed * IMPORT gen_nonce - * IMPORT csp_rule * IMPORT is_privileged_url - * IMPORT sanitize_attributes * IMPORT mozilla_suppress_scripts * IMPORT is_chrome * IMPORT is_mozilla * IMPORT start_activity_info_server + * IMPORT modify_on_the_fly * IMPORTS_END */ -/* - * Due to some technical limitations the chosen method of whitelisting sites - * is to smuggle whitelist indicator in page's url as a "magical" string - * after '#'. Right now this is only supplemental in HTTP(s) pages where - * blocking of native scripts also happens through CSP header injection but is - * necessary for protocols like ftp:// and file://. - * - * The code that actually injects the magical string into ftp:// and file:// - * urls has not yet been added to the extension. - */ - -var nonce = undefined; - -function handle_mutation(mutations, observer) -{ - if (document.readyState === 'complete') { - console.log("mutation handling complete"); - observer.disconnect(); - return; - } - for (const mutation of mutations) { - for (const node of mutation.addedNodes) - block_node(node); - } -} - -function block_nodes_recursively(node) -{ - block_node(node); - for (const child of node.children) - block_nodes_recursively(child); -} - -function block_node(node) +function accept_node(node, parent) { + const clone = document.importNode(node, false); + node.hachette_corresponding = clone; /* - * Modifying <script> element doesn't always prevent its execution in some - * Mozilla browsers. This is Chromium-specific code. + * TODO: Stop page's own issues like "Error parsing a meta element's + * content:" from appearing as extension's errors. */ - if (node.tagName === "SCRIPT") { - block_script(node); - return; - } - - sanitize_attributes(node); - - if (node.tagName === "HEAD") - inject_csp(node); -} - -function block_script(node) -{ - /* - * Disabling scripts this way allows them to still be relatively - * easily accessed in case they contain some useful data. - */ - if (node.hasAttribute("type")) - node.setAttribute("blocked-type", node.getAttribute("type")); - node.setAttribute("type", "application/json"); -} - -function inject_csp(head) -{ - let meta = document.createElement("meta"); - meta.setAttribute("http-equiv", "Content-Security-Policy"); - meta.setAttribute("content", csp_rule(nonce)); - - if (head.firstElementChild === null) - head.appendChild(meta); - else - head.insertBefore(meta, head.firstElementChild); + parent.hachette_corresponding.appendChild(clone); } if (!is_privileged_url(document.URL)) { @@ -110,20 +47,18 @@ if (!is_privileged_url(document.URL)) { handle_page_actions(policy.nonce); - if (!policy.allow) { - block_nodes_recursively(document.documentElement); + if (!policy.allow && is_mozilla) + addEventListener('beforescriptexecute', mozilla_suppress_scripts, true); - if (is_chrome) { - var observer = new MutationObserver(handle_mutation); - observer.observe(document.documentElement, { - attributes: true, - childList: true, - subtree: true - }); - } + if (!policy.allow && is_chrome) { + const old_html = document.documentElement; + const new_html = document.createElement("html"); + old_html.replaceWith(new_html); + old_html.hachette_corresponding = new_html; - if (is_mozilla) - addEventListener('beforescriptexecute', mozilla_suppress_scripts, true); + const modify_end = + modify_on_the_fly(old_html, policy, {node_eater: accept_node}); + document.addEventListener("DOMContentLoaded", modify_end); } start_activity_info_server(); |