aboutsummaryrefslogtreecommitdiff
path: root/content/page_actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'content/page_actions.js')
-rw-r--r--content/page_actions.js50
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});
+ }
}
/*