diff options
author | jahoti <jahoti@tilde.team> | 2021-12-03 00:00:00 +0000 |
---|---|---|
committer | jahoti <jahoti@tilde.team> | 2021-12-03 00:00:00 +0000 |
commit | d16e763e240a2aefe3d4490cddff61893a35a1ea (patch) | |
tree | 1e90890a39798f6cd9a1c0886d1234ccc187f5b3 /content/page_actions.js | |
parent | 591c48a6903bbf324361610f81c628302cae7049 (diff) | |
parent | 93dd73600e91eb19e11f5ca57f9429a85cf0150f (diff) | |
download | browser-extension-d16e763e240a2aefe3d4490cddff61893a35a1ea.tar.gz browser-extension-d16e763e240a2aefe3d4490cddff61893a35a1ea.zip |
Merge branch 'koszko' into jahoti
Diffstat (limited to 'content/page_actions.js')
-rw-r--r-- | content/page_actions.js | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/content/page_actions.js b/content/page_actions.js index aff56b8..845e452 100644 --- a/content/page_actions.js +++ b/content/page_actions.js @@ -1,5 +1,7 @@ /** - * Hachette handling of page actions in content scripts + * This file is part of Haketilo. + * + * Function: Handle page actions in a content script. * * Copyright (C) 2021 Wojtek Kosior * Redistribution terms are gathered in the `copyright' file. @@ -10,14 +12,17 @@ * IMPORT CONNECTION_TYPE * IMPORT browser * IMPORT report_script - * IMPORT report_settings + * IMPORT report_document_type * IMPORTS_END */ -var port; -var loaded = false; -var scripts_awaiting = []; -var nonce; +let policy; +/* Snapshot url and content type early; these can be changed by other code. */ +let url; +let is_html; +let port; +let loaded = false; +let scripts_awaiting = []; function handle_message(message) { @@ -31,11 +36,12 @@ function handle_message(message) scripts_awaiting.push(script_text); } } - if (action === "settings") - report_settings(data); + else { + console.error(`Bad page action '${action}'.`); + } } -function document_loaded(event) +function document_ready(event) { loaded = true; @@ -47,22 +53,32 @@ function document_loaded(event) function add_script(script_text) { + if (!is_html) + return; + let script = document.createElement("script"); script.textContent = script_text; - script.setAttribute("nonce", nonce); - script._hachette_payload = true; + script.setAttribute("nonce", policy.nonce); + script.haketilo_payload = true; document.body.appendChild(script); report_script(script_text); } -function handle_page_actions(script_nonce) { - document.addEventListener("DOMContentLoaded", document_loaded); - port = browser.runtime.connect({name : CONNECTION_TYPE.PAGE_ACTIONS}); - port.onMessage.addListener(handle_message); - port.postMessage({url: document.URL}); +function handle_page_actions(_policy, doc_ready_promise) { + policy = _policy; - nonce = script_nonce; + url = document.URL; + is_html = document instanceof HTMLDocument; + report_document_type(is_html); + + doc_ready_promise.then(document_ready); + + if (policy.payload) { + port = browser.runtime.connect({name : CONNECTION_TYPE.PAGE_ACTIONS}); + port.onMessage.addListener(handle_message); + port.postMessage({payload: policy.payload}); + } } /* |