Project

General

Profile

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

haketilo / background / page_actions_server.js @ 5957fbee

1
/**
2
 * Hachette serving of page actions to 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 get_storage
11
 * IMPORT TYPE_PREFIX
12
 * IMPORT CONNECTION_TYPE
13
 * IMPORT browser
14
 * IMPORT listen_for_connection
15
 * IMPORT sha256
16
 * IMPORT query_best
17
 * IMPORT make_ajax_request
18
 * IMPORTS_END
19
 */
20

    
21
var storage;
22
var handler;
23

    
24
function send_actions(url, port)
25
{
26
    const [pattern, settings] = query_best(storage, url);
27
    const repos = storage.get_all(TYPE_PREFIX.REPO);
28

    
29
    port.postMessage(["settings", [pattern, settings, repos]]);
30

    
31
    if (settings === undefined)
32
	return;
33

    
34
    let components = settings.components;
35
    let processed_bags = new Set();
36

    
37
    if (components !== undefined)
38
	send_scripts([components], port, processed_bags);
39
}
40

    
41
// TODO: parallelize script fetching
42
async function send_scripts(components, port, processed_bags)
43
{
44
    for (let [prefix, name] of components) {
45
	if (prefix === TYPE_PREFIX.BAG) {
46
	    if (processed_bags.has(name)) {
47
		console.log(`preventing recursive inclusion of bag ${name}`);
48
		continue;
49
	    }
50

    
51
	    var bag = storage.get(TYPE_PREFIX.BAG, name);
52

    
53
	    if (bag === undefined) {
54
		console.log(`no bag in storage for key ${name}`);
55
		continue;
56
	    }
57

    
58
	    processed_bags.add(name);
59
	    await send_scripts(bag, port, processed_bags);
60

    
61
	    processed_bags.delete(name);
62
	} else {
63
	    let script_text = await get_script_text(name);
64
	    if (script_text === undefined)
65
		continue;
66

    
67
	    port.postMessage(["inject", [script_text]]);
68
	}
69
    }
70
}
71

    
72
async function get_script_text(script_name)
73
{
74
    try {
75
	let script_data = storage.get(TYPE_PREFIX.SCRIPT, script_name);
76
	if (script_data === undefined) {
77
	    console.log(`missing data for ${script_name}`);
78
	    return;
79
	}
80
	let script_text = script_data.text;
81
	if (!script_text)
82
	    script_text = await fetch_remote_script(script_data);
83
	return script_text;
84
    } catch (e) {
85
	console.log(e);
86
    }
87
}
88

    
89
async function fetch_remote_script(script_data)
90
{
91
    try {
92
	let xhttp = await make_ajax_request("GET", script_data.url);
93
	if (xhttp.status === 200) {
94
	    let computed_hash = sha256(xhttp.responseText);
95
	    if (computed_hash !== script_data.hash) {
96
		console.log(`Bad hash for ${script_data.url}\n    got ${computed_hash} instead of ${script_data.hash}`);
97
		return;
98
	    }
99
	    return xhttp.responseText;
100
	} else {
101
	    console.log("script not fetched: " + script_data.url);
102
	    return;
103
	}
104
    } catch (e) {
105
	console.log(e);
106
    }
107
}
108

    
109
function handle_message(port, message, handler)
110
{
111
    port.onMessage.removeListener(handler[0]);
112
    let url = message.url;
113
    console.log({url});
114
    send_actions(url, port);
115
}
116

    
117
function new_connection(port)
118
{
119
    console.log("new page actions connection!");
120
    let handler = [];
121
    handler.push(m => handle_message(port, m, handler));
122
    port.onMessage.addListener(handler[0]);
123
}
124

    
125
async function start_page_actions_server()
126
{
127
    storage = await get_storage();
128

    
129
    listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection);
130
}
131

    
132
/*
133
 * EXPORTS_START
134
 * EXPORT start_page_actions_server
135
 * EXPORTS_END
136
 */
(2-2/6)