diff options
Diffstat (limited to 'content')
-rw-r--r-- | content/content.js | 51 | ||||
-rw-r--r-- | content/repo_query_cacher.js | 2 |
2 files changed, 48 insertions, 5 deletions
diff --git a/content/content.js b/content/content.js index c501187..120b16d 100644 --- a/content/content.js +++ b/content/content.js @@ -45,9 +45,34 @@ #FROM common/browser.js IMPORT browser #FROM common/misc.js IMPORT is_privileged_url -#FROM common/policy.js IMPORT decide_policy +#FROM common/policy.js IMPORT decide_policy, fallback_policy #FROM content/policy_enforcing.js IMPORT enforce_blocking +#IF CHROMIUM && MV2 +function synchronously_get_policy(url) +{ + const encoded_url = encodeURIComponent(url); + const request_url = `${browser.runtime.getURL("dummy")}?url=${encoded_url}`; + + try { + var xhttp = new XMLHttpRequest(); + xhttp.open("GET", request_url, false); + xhttp.send(); + } catch(e) { + console.error("Failure to synchronously fetch policy for url.", e); + return fallback_policy(); + } + + try { + const policy = /^[^?]*\?settings=(.*)$/.exec(xhttp.responseURL)[1]; + return JSON.parse(decodeURIComponent(policy)); + } catch(e) { + console.error("Failure to process synchronously fetched policy for url.", e); + return fallback_policy() + } +} +#ENDIF + let already_run = false, resolve_page_info, page_info_prom = new Promise(cb => resolve_page_info = cb); @@ -60,7 +85,11 @@ function on_page_info_request([type], sender, respond_cb) { return true; } +#IF MOZILLA || MV3 globalThis.haketilo_content_script_main = async function() { +#ELSE +async function main() { +#ENDIF if (already_run) return; @@ -72,10 +101,20 @@ globalThis.haketilo_content_script_main = async function() { browser.runtime.onMessage.addListener(on_page_info_request); repo_query_cacher.start(); - const policy = decide_policy(globalThis.haketilo_pattern_tree, - document.URL, - globalThis.haketilo_default_allow, - globalThis.haketilo_secret); +#IF MOZILLA || MV3 + try { + var policy = decide_policy(globalThis.haketilo_pattern_tree, + document.URL, + globalThis.haketilo_default_allow, + globalThis.haketilo_secret); + } catch(e) { + console.error(e); + var policy = fallback_policy(); + } +#ELSE + const policy = synchronously_get_policy(document.URL); +#ENDIF + const page_info = Object.assign({url: document.URL}, policy); ["csp", "nonce"].forEach(prop => delete page_info[prop]); @@ -108,10 +147,12 @@ globalThis.haketilo_content_script_main = async function() { resolve_page_info(page_info); } +#IF MOZILLA || MV3 function main() { if (globalThis.haketilo_pattern_tree !== undefined) globalThis.haketilo_content_script_main(); } +#ENDIF #IF !UNIT_TEST main(); diff --git a/content/repo_query_cacher.js b/content/repo_query_cacher.js index 41487e1..3f62be7 100644 --- a/content/repo_query_cacher.js +++ b/content/repo_query_cacher.js @@ -42,6 +42,8 @@ * proprietary program, I am not going to enforce this in court. */ +#FROM common/browser.js IMPORT browser + /* * Map URLs to objects containing parsed responses, error info or promises * resolving to those. |