Project

General

Profile

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

haketilo / content / page_actions.js @ 261548ff

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
 * IMPORTS_END
13
 */
14

    
15
var port;
16
var loaded = false;
17
var scripts_awaiting = [];
18
var nonce;
19

    
20
function handle_message(message)
21
{
22
    if (message.inject === undefined)
23
	return;
24

    
25
    for (let script_text of message.inject) {
26
	if (loaded)
27
	    add_script(script_text);
28
	else
29
	    scripts_awaiting.push(script_text);
30
    }
31
}
32

    
33
function document_loaded(event)
34
{
35
    loaded = true;
36

    
37
    for (let script_text of scripts_awaiting)
38
	add_script(script_text);
39

    
40
    scripts_awaiting = undefined;
41
}
42

    
43
function add_script(script_text)
44
{
45
    let script = document.createElement("script");
46
    script.textContent = script_text;
47
    script.setAttribute("nonce", nonce);
48
    document.body.appendChild(script);
49
}
50

    
51
function handle_page_actions(script_nonce) {
52
    document.addEventListener("DOMContentLoaded", document_loaded);
53
    port = browser.runtime.connect({name : CONNECTION_TYPE.PAGE_ACTIONS});
54
    port.onMessage.addListener(handle_message);
55
    port.postMessage({url: document.URL});
56

    
57
    nonce = script_nonce;
58
}
59

    
60
/*
61
 * EXPORTS_START
62
 * EXPORT handle_page_actions
63
 * EXPORTS_END
64
 */
(3-3/3)