/** * Hachette main content script run in all frames * * Copyright (C) 2021 Wojtek Kosior * Copyright (C) 2021 jahoti * Redistribution terms are gathered in the `copyright' file. */ /* * IMPORTS_START * IMPORT handle_page_actions * IMPORT extract_signed * IMPORT gen_nonce * IMPORT is_privileged_url * IMPORT mozilla_suppress_scripts * IMPORT is_chrome * IMPORT is_mozilla * IMPORT start_activity_info_server * IMPORT modify_on_the_fly * IMPORTS_END */ function accept_node(node, parent) { const clone = document.importNode(node, false); node.hachette_corresponding = clone; /* * TODO: Stop page's own issues like "Error parsing a meta element's * content:" from appearing as extension's errors. */ parent.hachette_corresponding.appendChild(clone); } if (!is_privileged_url(document.URL)) { /* Signature valid for half an hour. */ const min_time = new Date().getTime() - 1800 * 1000; let best_result = {time: -1}; let policy = null; const extracted_signatures = []; for (const match of document.cookie.matchAll(/hachette-(\w*)=([^;]*)/g)) { const new_result = extract_signed(...match.slice(1, 3)); if (new_result.fail) continue; extracted_signatures.push(match[1]); if (new_result.time < Math.max(min_time, best_result.time)) continue; /* This should succeed - it's our self-produced valid JSON. */ const new_policy = JSON.parse(decodeURIComponent(new_result.data)); if (new_policy.url !== document.URL) continue; best_result = new_result; policy = new_policy; } if (!policy) { console.warn("WARNING! Using default policy!!!"); policy = {allow: false, nonce: gen_nonce()}; } for (const signature of extracted_signatures) document.cookie = `hachette-${signature}=; Max-Age=-1;`; handle_page_actions(policy.nonce); if (!policy.allow) { const old_html = document.documentElement; const new_html = document.createElement("html"); old_html.replaceWith(new_html); old_html.hachette_corresponding = new_html; const modify_end = modify_on_the_fly(old_html, policy, {node_eater: accept_node}); document.addEventListener("DOMContentLoaded", modify_end); } start_activity_info_server(); }