Project

General

Profile

Download (1.41 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / content / page_actions.js @ b7e2870f

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 CONNECTION_TYPE
11
 * IMPORT browser
12
 * IMPORT report_script
13
 * IMPORT report_settings
14
 * IMPORTS_END
15
 */
16

    
17
var port;
18
var loaded = false;
19
var scripts_awaiting = [];
20
var nonce;
21

    
22
function handle_message(message)
23
{
24
    const [action, data] = message;
25

    
26
    if (action === "inject") {
27
	for (let script_text of data) {
28
	    if (loaded)
29
		add_script(script_text);
30
	    else
31
		scripts_awaiting.push(script_text);
32
	}
33
    }
34
    if (action === "settings")
35
	report_settings(data);
36
}
37

    
38
function document_loaded(event)
39
{
40
    loaded = true;
41

    
42
    for (let script_text of scripts_awaiting)
43
	add_script(script_text);
44

    
45
    scripts_awaiting = undefined;
46
}
47

    
48
function add_script(script_text)
49
{
50
    let script = document.createElement("script");
51
    script.textContent = script_text;
52
    script.setAttribute("nonce", nonce);
53
    document.body.appendChild(script);
54

    
55
    report_script(script_text);
56
}
57

    
58
function handle_page_actions(script_nonce) {
59
    document.addEventListener("DOMContentLoaded", document_loaded);
60
    port = browser.runtime.connect({name : CONNECTION_TYPE.PAGE_ACTIONS});
61
    port.onMessage.addListener(handle_message);
62
    port.postMessage({url: document.URL});
63

    
64
    nonce = script_nonce;
65
}
66

    
67
/*
68
 * EXPORTS_START
69
 * EXPORT handle_page_actions
70
 * EXPORTS_END
71
 */
(4-4/4)