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 get_query_best
|
17
|
* IMPORT make_ajax_request
|
18
|
* IMPORTS_END
|
19
|
*/
|
20
|
|
21
|
var storage;
|
22
|
var query_best;
|
23
|
var handler;
|
24
|
|
25
|
function send_actions(url, port)
|
26
|
{
|
27
|
const [pattern, settings] = query_best(url);
|
28
|
const repos = storage.get_all(TYPE_PREFIX.REPO);
|
29
|
|
30
|
port.postMessage(["settings", [pattern, settings, repos]]);
|
31
|
|
32
|
if (settings === undefined)
|
33
|
return;
|
34
|
|
35
|
let components = settings.components;
|
36
|
let processed_bags = new Set();
|
37
|
|
38
|
if (components !== undefined)
|
39
|
send_scripts([components], port, processed_bags);
|
40
|
}
|
41
|
|
42
|
// TODO: parallelize script fetching
|
43
|
async function send_scripts(components, port, processed_bags)
|
44
|
{
|
45
|
for (let [prefix, name] of components) {
|
46
|
if (prefix === TYPE_PREFIX.BAG) {
|
47
|
if (processed_bags.has(name)) {
|
48
|
console.log(`preventing recursive inclusion of bag ${name}`);
|
49
|
continue;
|
50
|
}
|
51
|
|
52
|
var bag = storage.get(TYPE_PREFIX.BAG, name);
|
53
|
|
54
|
if (bag === undefined) {
|
55
|
console.log(`no bag in storage for key ${name}`);
|
56
|
continue;
|
57
|
}
|
58
|
|
59
|
processed_bags.add(name);
|
60
|
await send_scripts(bag, port, processed_bags);
|
61
|
|
62
|
processed_bags.delete(name);
|
63
|
} else {
|
64
|
let script_text = await get_script_text(name);
|
65
|
if (script_text === undefined)
|
66
|
continue;
|
67
|
|
68
|
port.postMessage(["inject", [script_text]]);
|
69
|
}
|
70
|
}
|
71
|
}
|
72
|
|
73
|
async function get_script_text(script_name)
|
74
|
{
|
75
|
try {
|
76
|
let script_data = storage.get(TYPE_PREFIX.SCRIPT, script_name);
|
77
|
if (script_data === undefined) {
|
78
|
console.log(`missing data for ${script_name}`);
|
79
|
return;
|
80
|
}
|
81
|
let script_text = script_data.text;
|
82
|
if (!script_text)
|
83
|
script_text = await fetch_remote_script(script_data);
|
84
|
return script_text;
|
85
|
} catch (e) {
|
86
|
console.log(e);
|
87
|
}
|
88
|
}
|
89
|
|
90
|
async function fetch_remote_script(script_data)
|
91
|
{
|
92
|
try {
|
93
|
let xhttp = await make_ajax_request("GET", script_data.url);
|
94
|
if (xhttp.status === 200) {
|
95
|
let computed_hash = sha256(xhttp.responseText);
|
96
|
if (computed_hash !== script_data.hash) {
|
97
|
console.log(`Bad hash for ${script_data.url}\n got ${computed_hash} instead of ${script_data.hash}`);
|
98
|
return;
|
99
|
}
|
100
|
return xhttp.responseText;
|
101
|
} else {
|
102
|
console.log("script not fetched: " + script_data.url);
|
103
|
return;
|
104
|
}
|
105
|
} catch (e) {
|
106
|
console.log(e);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
function handle_message(port, message, handler)
|
111
|
{
|
112
|
port.onMessage.removeListener(handler[0]);
|
113
|
let url = message.url;
|
114
|
console.log({url});
|
115
|
send_actions(url, port);
|
116
|
}
|
117
|
|
118
|
function new_connection(port)
|
119
|
{
|
120
|
console.log("new page actions connection!");
|
121
|
let handler = [];
|
122
|
handler.push(m => handle_message(port, m, handler));
|
123
|
port.onMessage.addListener(handler[0]);
|
124
|
}
|
125
|
|
126
|
async function start_page_actions_server()
|
127
|
{
|
128
|
storage = await get_storage();
|
129
|
query_best = await get_query_best();
|
130
|
|
131
|
listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection);
|
132
|
}
|
133
|
|
134
|
/*
|
135
|
* EXPORTS_START
|
136
|
* EXPORT start_page_actions_server
|
137
|
* EXPORTS_END
|
138
|
*/
|