Project

General

Profile

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

haketilo / content / page_actions.js @ b93f26bf

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
"use strict";
9

    
10
(() => {
11
    const CONNECTION_TYPE = window.CONNECTION_TYPE;
12
    const browser = window.browser;
13

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

    
19
    function handle_message(message)
20
    {
21
	console.log(["message", message]);
22

    
23
	if (message.inject === undefined)
24
	    return;
25

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

    
34
    function document_loaded(event)
35
    {
36
	console.log("loaded");
37

    
38
	loaded = true;
39

    
40
	for (let script_text of scripts_awaiting)
41
	    add_script(script_text);
42

    
43
	scripts_awaiting = undefined;
44
    }
45

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

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

    
60
	nonce = script_nonce;
61
    }
62

    
63
    window.handle_page_actions = handle_page_actions;
64
})();
(2-2/2)