1
|
/**
|
2
|
* Hachette display panel logic
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
* Redistribution terms are gathered in the `copyright' file.
|
6
|
*/
|
7
|
|
8
|
/*
|
9
|
* IMPORTS_START
|
10
|
* IMPORT browser
|
11
|
* IMPORT is_chrome
|
12
|
* IMPORT is_mozilla
|
13
|
*** Using remote storage here seems inefficient, we only resort to that
|
14
|
*** temporarily, before all storage access gets reworked.
|
15
|
* IMPORT get_remote_storage
|
16
|
* IMPORT get_import_frame
|
17
|
* IMPORT init_default_policy_dialog
|
18
|
* IMPORT query_all
|
19
|
* IMPORT CONNECTION_TYPE
|
20
|
* IMPORT is_privileged_url
|
21
|
* IMPORT TYPE_PREFIX
|
22
|
* IMPORT nice_name
|
23
|
* IMPORT open_in_settings
|
24
|
* IMPORT each_url_pattern
|
25
|
* IMPORT by_id
|
26
|
* IMPORT clone_template
|
27
|
* IMPORTS_END
|
28
|
*/
|
29
|
|
30
|
let storage;
|
31
|
let tab_url;
|
32
|
|
33
|
/* Force popup <html>'s reflow on stupid Firefox. */
|
34
|
if (is_mozilla) {
|
35
|
const reflow_forcer =
|
36
|
() => document.documentElement.style.width = "-moz-fit-content";
|
37
|
for (const radio of document.querySelectorAll('[name="current_view"]'))
|
38
|
radio.addEventListener("change", reflow_forcer);
|
39
|
}
|
40
|
|
41
|
const show_queried_view_radio = by_id("show_queried_view_radio");
|
42
|
|
43
|
const tab_query = {currentWindow: true, active: true};
|
44
|
|
45
|
async function get_current_tab()
|
46
|
{
|
47
|
/* Fix for fact that Chrome does not use promises here */
|
48
|
const promise = is_chrome ?
|
49
|
new Promise((resolve, reject) =>
|
50
|
browser.tabs.query(tab_query, tab => resolve(tab))) :
|
51
|
browser.tabs.query(tab_query);
|
52
|
|
53
|
try {
|
54
|
return (await promise)[0];
|
55
|
} catch(e) {
|
56
|
console.log(e);
|
57
|
}
|
58
|
}
|
59
|
|
60
|
const page_url_heading = by_id("page_url_heading");
|
61
|
const privileged_notice = by_id("privileged_notice");
|
62
|
const page_state = by_id("page_state");
|
63
|
|
64
|
/* Helper functions to convert string into a list of one-letter <span>'s. */
|
65
|
function char_to_span(char, doc)
|
66
|
{
|
67
|
const span = document.createElement("span");
|
68
|
span.textContent = char;
|
69
|
return span;
|
70
|
}
|
71
|
|
72
|
function to_spans(string, doc=document)
|
73
|
{
|
74
|
return string.split("").map(c => char_to_span(c, doc));
|
75
|
}
|
76
|
|
77
|
async function show_page_activity_info()
|
78
|
{
|
79
|
const tab = await get_current_tab();
|
80
|
|
81
|
if (tab === undefined) {
|
82
|
page_url_heading.textContent = "unknown page";
|
83
|
return;
|
84
|
}
|
85
|
|
86
|
tab_url = /^([^?#]*)/.exec(tab.url)[1];
|
87
|
to_spans(tab_url).forEach(s => page_url_heading.append(s));
|
88
|
if (is_privileged_url(tab_url)) {
|
89
|
privileged_notice.classList.remove("hide");
|
90
|
return;
|
91
|
}
|
92
|
|
93
|
populate_possible_patterns_list(tab_url);
|
94
|
page_state.classList.remove("hide");
|
95
|
|
96
|
try_to_connect(tab.id);
|
97
|
}
|
98
|
|
99
|
const possible_patterns_list = by_id("possible_patterns");
|
100
|
const known_patterns = new Map();
|
101
|
|
102
|
function add_pattern_to_list(pattern)
|
103
|
{
|
104
|
const template = clone_template("pattern_entry");
|
105
|
template.name.textContent = pattern;
|
106
|
|
107
|
const settings_opener = () => open_in_settings(TYPE_PREFIX.PAGE, pattern);
|
108
|
template.button.addEventListener("click", settings_opener);
|
109
|
|
110
|
known_patterns.set(pattern, template);
|
111
|
possible_patterns_list.append(template.entry);
|
112
|
|
113
|
return template;
|
114
|
}
|
115
|
|
116
|
function style_possible_pattern_entry(pattern, exists_in_settings)
|
117
|
{
|
118
|
const [text, class_action] = exists_in_settings ?
|
119
|
["Edit", "add"] : ["Add", "remove"];
|
120
|
const entry_object = known_patterns.get(pattern);
|
121
|
|
122
|
if (entry_object) {
|
123
|
entry_object.button.textContent = `${text} setting`;
|
124
|
entry_object.entry.classList[class_action]("matched_pattern");
|
125
|
}
|
126
|
}
|
127
|
|
128
|
function handle_page_change(change)
|
129
|
{
|
130
|
style_possible_pattern_entry(change.item, change.new_val !== undefined);
|
131
|
}
|
132
|
|
133
|
function populate_possible_patterns_list(url)
|
134
|
{
|
135
|
for (const pattern of each_url_pattern(url))
|
136
|
add_pattern_to_list(pattern);
|
137
|
|
138
|
for (const [pattern, settings] of query_all(storage, url))
|
139
|
style_possible_pattern_entry(pattern, true);
|
140
|
|
141
|
storage.add_change_listener(handle_page_change, [TYPE_PREFIX.PAGE]);
|
142
|
}
|
143
|
|
144
|
const connected_chbx = by_id("connected_chbx");
|
145
|
const query_pattern_but = by_id("query_pattern");
|
146
|
|
147
|
var content_script_port;
|
148
|
|
149
|
function try_to_connect(tab_id)
|
150
|
{
|
151
|
/* This won't connect to iframes. We'll add support for them later */
|
152
|
const connect_info = {name: CONNECTION_TYPE.ACTIVITY_INFO, frameId: 0};
|
153
|
content_script_port = browser.tabs.connect(tab_id, connect_info);
|
154
|
|
155
|
const disconnect_cb = () => handle_disconnect(tab_id, start_querying_repos);
|
156
|
content_script_port.onDisconnect.addListener(disconnect_cb);
|
157
|
content_script_port.onMessage.addListener(handle_activity_report);
|
158
|
|
159
|
query_pattern_but.addEventListener("click", start_querying_repos);
|
160
|
|
161
|
if (is_mozilla)
|
162
|
setTimeout(() => monitor_connecting(tab_id), 1000);
|
163
|
}
|
164
|
|
165
|
function start_querying_repos()
|
166
|
{
|
167
|
query_pattern_but.removeEventListener("click", start_querying_repos);
|
168
|
const repo_urls = storage.get_all_names(TYPE_PREFIX.REPO);
|
169
|
if (content_script_port)
|
170
|
content_script_port.postMessage([TYPE_PREFIX.URL, tab_url, repo_urls]);
|
171
|
}
|
172
|
|
173
|
const loading_point = by_id("loading_point");
|
174
|
const reload_notice = by_id("reload_notice");
|
175
|
|
176
|
function handle_disconnect(tab_id, button_cb)
|
177
|
{
|
178
|
query_pattern_but.removeEventListener("click", button_cb);
|
179
|
content_script_port = null;
|
180
|
|
181
|
if (is_chrome && !browser.runtime.lastError)
|
182
|
return;
|
183
|
|
184
|
/* return if error was not during connection initialization */
|
185
|
if (connected_chbx.checked)
|
186
|
return;
|
187
|
|
188
|
loading_point.classList.toggle("camouflage");
|
189
|
reload_notice.classList.remove("hide");
|
190
|
|
191
|
setTimeout(() => try_to_connect(tab_id), 1000);
|
192
|
}
|
193
|
|
194
|
function monitor_connecting(tab_id)
|
195
|
{
|
196
|
if (connected_chbx.checked)
|
197
|
return;
|
198
|
|
199
|
if (content_script_port)
|
200
|
content_script_port.disconnect();
|
201
|
else
|
202
|
return;
|
203
|
|
204
|
loading_point.classList.toggle("camouflage");
|
205
|
reload_notice.classList.remove("hide");
|
206
|
try_to_connect(tab_id);
|
207
|
}
|
208
|
|
209
|
const pattern_span = by_id("pattern");
|
210
|
const view_pattern_but = by_id("view_pattern");
|
211
|
const blocked_span = by_id("blocked");
|
212
|
const payload_span = by_id("payload");
|
213
|
const payload_buttons_div = by_id("payload_buttons");
|
214
|
const view_payload_but = by_id("view_payload");
|
215
|
const view_injected_but = by_id("view_injected");
|
216
|
const container_for_injected = by_id("container_for_injected");
|
217
|
const content_type_cell = by_id("content_type");
|
218
|
|
219
|
const queried_items = new Map();
|
220
|
|
221
|
let max_injected_script_id = 0;
|
222
|
|
223
|
function handle_activity_report(message)
|
224
|
{
|
225
|
connected_chbx.checked = true;
|
226
|
|
227
|
const [type, data] = message;
|
228
|
|
229
|
if (type === "settings") {
|
230
|
let [pattern, settings] = data;
|
231
|
|
232
|
blocked_span.textContent = settings.allow ? "no" : "yes";
|
233
|
|
234
|
if (pattern) {
|
235
|
pattern_span.textContent = pattern;
|
236
|
const settings_opener =
|
237
|
() => open_in_settings(TYPE_PREFIX.PAGE, pattern);
|
238
|
view_pattern_but.classList.remove("hide");
|
239
|
view_pattern_but.addEventListener("click", settings_opener);
|
240
|
} else {
|
241
|
pattern_span.textContent = "none";
|
242
|
blocked_span.textContent = blocked_span.textContent + " (default)";
|
243
|
}
|
244
|
|
245
|
const components = settings.components;
|
246
|
if (components) {
|
247
|
payload_span.textContent = nice_name(...components);
|
248
|
payload_buttons_div.classList.remove("hide");
|
249
|
const settings_opener = () => open_in_settings(...components);
|
250
|
view_payload_but.addEventListener("click", settings_opener);
|
251
|
} else {
|
252
|
payload_span.textContent = "none";
|
253
|
}
|
254
|
}
|
255
|
if (type === "script") {
|
256
|
const template = clone_template("injected_script");
|
257
|
const chbx_id = `injected_script_${max_injected_script_id++}`;
|
258
|
template.chbx.id = chbx_id;
|
259
|
template.lbl.setAttribute("for", chbx_id);
|
260
|
template.script_contents.textContent = data;
|
261
|
container_for_injected.appendChild(template.div);
|
262
|
}
|
263
|
if (type === "is_html") {
|
264
|
if (!data)
|
265
|
content_type_cell.classList.remove("hide");
|
266
|
}
|
267
|
if (type === "repo_query_action") {
|
268
|
const key = data.prefix + data.item;
|
269
|
const results = queried_items.get(key) || {};
|
270
|
Object.assign(results, data.results);
|
271
|
queried_items.set(key, results);
|
272
|
|
273
|
const action = data.prefix === TYPE_PREFIX.URL ?
|
274
|
show_query_result : record_fetched_install_dep;
|
275
|
|
276
|
for (const [repo_url, result] of Object.entries(data.results))
|
277
|
action(data.prefix, data.item, repo_url, result);
|
278
|
}
|
279
|
}
|
280
|
|
281
|
const container_for_repo_responses = by_id("container_for_repo_responses");
|
282
|
|
283
|
const results_lists = new Map();
|
284
|
|
285
|
function create_results_list(url)
|
286
|
{
|
287
|
const cloned_template = clone_template("multi_repos_query_result");
|
288
|
cloned_template.url_span.textContent = url;
|
289
|
container_for_repo_responses.appendChild(cloned_template.div);
|
290
|
|
291
|
cloned_template.by_repo = new Map();
|
292
|
results_lists.set(url, cloned_template);
|
293
|
|
294
|
return cloned_template;
|
295
|
}
|
296
|
|
297
|
function create_result_item(list_object, repo_url, result)
|
298
|
{
|
299
|
const cloned_template = clone_template("single_repo_query_result");
|
300
|
cloned_template.repo_url.textContent = repo_url;
|
301
|
cloned_template.appended = null;
|
302
|
|
303
|
list_object.ul.appendChild(cloned_template.li);
|
304
|
list_object.by_repo.set(repo_url, cloned_template);
|
305
|
|
306
|
return cloned_template;
|
307
|
}
|
308
|
|
309
|
function set_appended(result_item, element)
|
310
|
{
|
311
|
if (result_item.appended)
|
312
|
result_item.appended.remove();
|
313
|
result_item.appended = element;
|
314
|
result_item.li.appendChild(element);
|
315
|
}
|
316
|
|
317
|
function show_message(result_item, text)
|
318
|
{
|
319
|
const div = document.createElement("div");
|
320
|
div.textContent = text;
|
321
|
set_appended(result_item, div);
|
322
|
}
|
323
|
|
324
|
function showcb(text)
|
325
|
{
|
326
|
return item => show_message(item, text);
|
327
|
}
|
328
|
|
329
|
function unroll_chbx_first_checked(entry_object)
|
330
|
{
|
331
|
if (!entry_object.chbx.checked)
|
332
|
return;
|
333
|
|
334
|
entry_object.chbx.removeEventListener("change", entry_object.unroll_cb);
|
335
|
delete entry_object.unroll_cb;
|
336
|
|
337
|
entry_object.unroll.innerHTML = "preview not implemented...<br />(consider contributing)";
|
338
|
}
|
339
|
|
340
|
let import_frame;
|
341
|
let install_target = null;
|
342
|
|
343
|
function install_abort(error_state)
|
344
|
{
|
345
|
import_frame.show_error(`Error: ${error_state}`);
|
346
|
install_target = null;
|
347
|
}
|
348
|
|
349
|
/*
|
350
|
* Translate objects from the format in which they are sent by Hydrilla to the
|
351
|
* format in which they are stored in settings.
|
352
|
*/
|
353
|
|
354
|
function translate_script(script_object, repo_url)
|
355
|
{
|
356
|
return {
|
357
|
[TYPE_PREFIX.SCRIPT + script_object.name]: {
|
358
|
hash: script_object.sha256,
|
359
|
url: `${repo_url}/content/${script_object.location}`
|
360
|
}
|
361
|
};
|
362
|
}
|
363
|
|
364
|
function translate_bag(bag_object)
|
365
|
{
|
366
|
return {
|
367
|
[TYPE_PREFIX.BAG + bag_object.name]: bag_object.components
|
368
|
};
|
369
|
}
|
370
|
|
371
|
const format_translators = {
|
372
|
[TYPE_PREFIX.BAG]: translate_bag,
|
373
|
[TYPE_PREFIX.SCRIPT]: translate_script
|
374
|
};
|
375
|
|
376
|
function install_check_ready()
|
377
|
{
|
378
|
if (install_target.to_fetch.size > 0)
|
379
|
return;
|
380
|
|
381
|
const page_key = [TYPE_PREFIX.PAGE + install_target.pattern];
|
382
|
const to_install = [{[page_key]: {components: install_target.payload}}];
|
383
|
|
384
|
for (const key of install_target.fetched) {
|
385
|
const old_object =
|
386
|
queried_items.get(key)[install_target.repo_url].response;
|
387
|
const new_object =
|
388
|
format_translators[key[0]](old_object, install_target.repo_url);
|
389
|
to_install.push(new_object);
|
390
|
}
|
391
|
|
392
|
import_frame.show_selection(to_install);
|
393
|
}
|
394
|
|
395
|
const possible_errors = ["connection_error", "parse_error"];
|
396
|
|
397
|
function fetch_install_deps(components)
|
398
|
{
|
399
|
const needed = [...components];
|
400
|
const processed = new Set();
|
401
|
|
402
|
while (needed.length > 0) {
|
403
|
const [prefix, item] = needed.pop();
|
404
|
const key = prefix + item;
|
405
|
processed.add(key);
|
406
|
const results = queried_items.get(key);
|
407
|
let relevant_result = null;
|
408
|
|
409
|
if (results)
|
410
|
relevant_result = results[install_target.repo_url];
|
411
|
|
412
|
if (!relevant_result) {
|
413
|
content_script_port.postMessage([prefix, item,
|
414
|
[install_target.repo_url]]);
|
415
|
install_target.to_fetch.add(key);
|
416
|
continue;
|
417
|
}
|
418
|
|
419
|
if (possible_errors.includes(relevant_result.state)) {
|
420
|
install_abort(relevant_result.state);
|
421
|
return false;
|
422
|
}
|
423
|
|
424
|
install_target.fetched.add(key);
|
425
|
|
426
|
if (prefix !== TYPE_PREFIX.BAG)
|
427
|
continue;
|
428
|
|
429
|
for (const dependency of relevant_result.response.components) {
|
430
|
if (processed.has(dependency.join('')))
|
431
|
continue;
|
432
|
needed.push(dependency);
|
433
|
}
|
434
|
}
|
435
|
}
|
436
|
|
437
|
function record_fetched_install_dep(prefix, item, repo_url, result)
|
438
|
{
|
439
|
const key = prefix + item;
|
440
|
|
441
|
if (!install_target || repo_url !== install_target.repo_url ||
|
442
|
!install_target.to_fetch.has(key))
|
443
|
return;
|
444
|
|
445
|
if (possible_errors.includes(result.state)) {
|
446
|
install_abort(result.state);
|
447
|
return;
|
448
|
}
|
449
|
|
450
|
if (result.state !== "completed")
|
451
|
return;
|
452
|
|
453
|
install_target.to_fetch.delete(key);
|
454
|
install_target.fetched.add(key);
|
455
|
|
456
|
if (prefix === TYPE_PREFIX.BAG &&
|
457
|
fetch_install_deps(result.response.components) === false)
|
458
|
return;
|
459
|
|
460
|
install_check_ready();
|
461
|
}
|
462
|
|
463
|
function install_clicked(entry_object)
|
464
|
{
|
465
|
import_frame.show_loading();
|
466
|
|
467
|
install_target = {
|
468
|
repo_url: entry_object.repo_url,
|
469
|
pattern: entry_object.match_object.pattern,
|
470
|
payload: entry_object.match_object.payload,
|
471
|
fetched: new Set(),
|
472
|
to_fetch: new Set()
|
473
|
};
|
474
|
|
475
|
fetch_install_deps([install_target.payload]);
|
476
|
|
477
|
install_check_ready();
|
478
|
}
|
479
|
|
480
|
var max_query_result_id = 0;
|
481
|
|
482
|
function show_query_successful_result(result_item, repo_url, result)
|
483
|
{
|
484
|
if (result.length === 0) {
|
485
|
show_message(result_item, "No results :(");
|
486
|
return;
|
487
|
}
|
488
|
|
489
|
const cloned_ul_template = clone_template("result_patterns_list");
|
490
|
set_appended(result_item, cloned_ul_template.ul);
|
491
|
|
492
|
for (const match of result) {
|
493
|
const entry_object = clone_template("query_match_li");
|
494
|
|
495
|
entry_object.pattern.textContent = match.pattern;
|
496
|
|
497
|
cloned_ul_template.ul.appendChild(entry_object.li);
|
498
|
|
499
|
if (!match.payload) {
|
500
|
entry_object.payload.textContent = "(none)";
|
501
|
for (const key of ["chbx", "triangle", "unroll"])
|
502
|
entry_object[key].remove();
|
503
|
continue;
|
504
|
}
|
505
|
|
506
|
entry_object.payload.textContent = nice_name(...match.payload);
|
507
|
|
508
|
const install_cb = () => install_clicked(entry_object);
|
509
|
entry_object.btn.addEventListener("click", install_cb);
|
510
|
|
511
|
const chbx_id = `query_result_${max_query_result_id++}`;
|
512
|
entry_object.chbx.id = chbx_id;
|
513
|
entry_object.lbl.setAttribute("for", chbx_id);
|
514
|
|
515
|
entry_object.unroll_cb = () => unroll_chbx_first_checked(entry_object);
|
516
|
entry_object.chbx.addEventListener("change", entry_object.unroll_cb);
|
517
|
|
518
|
entry_object.component_object = match.payload;
|
519
|
entry_object.match_object = match;
|
520
|
entry_object.repo_url = repo_url;
|
521
|
}
|
522
|
}
|
523
|
|
524
|
function show_query_result(url_prefix, url, repo_url, result)
|
525
|
{
|
526
|
const results_list_object = results_lists.get(url) ||
|
527
|
create_results_list(url);
|
528
|
const result_item = results_list_object.by_repo.get(repo_url) ||
|
529
|
create_result_item(results_list_object, repo_url, result);
|
530
|
|
531
|
const completed_cb =
|
532
|
item => show_query_successful_result(item, repo_url, result.response);
|
533
|
const possible_actions = {
|
534
|
completed: completed_cb,
|
535
|
started: showcb("loading..."),
|
536
|
connection_error: showcb("Error when querying repository."),
|
537
|
parse_error: showcb("Bad data format received.")
|
538
|
};
|
539
|
possible_actions[result.state](result_item, repo_url);
|
540
|
}
|
541
|
|
542
|
by_id("settings_but")
|
543
|
.addEventListener("click", (e) => browser.runtime.openOptionsPage());
|
544
|
|
545
|
async function main()
|
546
|
{
|
547
|
init_default_policy_dialog();
|
548
|
|
549
|
storage = await get_remote_storage();
|
550
|
import_frame = await get_import_frame();
|
551
|
import_frame.onclose = () => show_queried_view_radio.checked = true;
|
552
|
show_page_activity_info();
|
553
|
}
|
554
|
|
555
|
main();
|