1
|
/**
|
2
|
* Myext handling of page actions in content scripts
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
* Redistribution terms are gathered in the `copyright' file.
|
6
|
*/
|
7
|
|
8
|
/*
|
9
|
* IMPORTS_START
|
10
|
* IMPORT browser
|
11
|
* IMPORT report_script
|
12
|
* IMPORT report_settings
|
13
|
* IMPORTS_END
|
14
|
*/
|
15
|
|
16
|
var port;
|
17
|
var loaded = false;
|
18
|
var scripts_awaiting = [];
|
19
|
var nonce;
|
20
|
|
21
|
function handle_message(message)
|
22
|
{
|
23
|
const [action, data] = message;
|
24
|
|
25
|
if (action === "inject") {
|
26
|
for (let script_text of data) {
|
27
|
if (loaded)
|
28
|
add_script(script_text);
|
29
|
else
|
30
|
scripts_awaiting.push(script_text);
|
31
|
}
|
32
|
}
|
33
|
if (action === "settings")
|
34
|
report_settings(data);
|
35
|
}
|
36
|
|
37
|
function document_loaded(event)
|
38
|
{
|
39
|
loaded = true;
|
40
|
|
41
|
for (let script_text of scripts_awaiting)
|
42
|
add_script(script_text);
|
43
|
|
44
|
scripts_awaiting = undefined;
|
45
|
}
|
46
|
|
47
|
function add_script(script_text)
|
48
|
{
|
49
|
let script = document.createElement("script");
|
50
|
script.textContent = script_text;
|
51
|
script.setAttribute("nonce", nonce);
|
52
|
document.body.appendChild(script);
|
53
|
|
54
|
report_script(script_text);
|
55
|
}
|
56
|
|
57
|
function handle_page_actions(script_nonce, port) { // Add port as an argument so we can "pre-receive" a nonce in main.js
|
58
|
document.addEventListener("DOMContentLoaded", document_loaded);
|
59
|
port.onMessage.addListener(handle_message);
|
60
|
port.postMessage({url: document.URL});
|
61
|
|
62
|
nonce = script_nonce;
|
63
|
}
|
64
|
|
65
|
/*
|
66
|
* EXPORTS_START
|
67
|
* EXPORT handle_page_actions
|
68
|
* EXPORTS_END
|
69
|
*/
|