1
|
/**
|
2
|
* part of Hachette
|
3
|
* Informing about activities performed by content script (script injection,
|
4
|
* script blocking).
|
5
|
*
|
6
|
* Copyright (C) 2021 Wojtek Kosior
|
7
|
* Redistribution terms are gathered in the `copyright' file.
|
8
|
*/
|
9
|
|
10
|
/*
|
11
|
* IMPORTS_START
|
12
|
* IMPORT listen_for_connection
|
13
|
* IMPORT CONNECTION_TYPE
|
14
|
* IMPORT set_repo_query_repos
|
15
|
* IMPORT set_repo_query_callback
|
16
|
* IMPORTS_END
|
17
|
*/
|
18
|
|
19
|
var activities = [];
|
20
|
var ports = new Set();
|
21
|
|
22
|
function report_activity(name, data)
|
23
|
{
|
24
|
const activity = [name, data];
|
25
|
activities.push(activity);
|
26
|
|
27
|
for (const port of ports)
|
28
|
port.postMessage(activity);
|
29
|
}
|
30
|
|
31
|
function report_script(script_data)
|
32
|
{
|
33
|
report_activity("script", script_data);
|
34
|
}
|
35
|
|
36
|
function report_settings(settings)
|
37
|
{
|
38
|
const [pattern, settings_data, repos] = settings;
|
39
|
set_repo_query_repos(repos);
|
40
|
|
41
|
report_activity("settings", settings);
|
42
|
}
|
43
|
|
44
|
function report_repo_query_result(result)
|
45
|
{
|
46
|
report_activity("repo_query_result", result);
|
47
|
}
|
48
|
|
49
|
function trigger_repo_query()
|
50
|
{
|
51
|
set_repo_query_callback(report_repo_query_result);
|
52
|
}
|
53
|
|
54
|
function new_connection(port)
|
55
|
{
|
56
|
console.log("new activity info connection!");
|
57
|
|
58
|
ports.add(port);
|
59
|
|
60
|
for (const activity of activities)
|
61
|
port.postMessage(activity);
|
62
|
|
63
|
/*
|
64
|
* So far the only thing we expect to receive is repo query order. Once more
|
65
|
* possibilities arrive, we will need to complicate this listener.
|
66
|
*/
|
67
|
port.onMessage.addListener(trigger_repo_query);
|
68
|
|
69
|
port.onDisconnect.addListener(() => ports.delete(port));
|
70
|
}
|
71
|
|
72
|
function start_activity_info_server()
|
73
|
{
|
74
|
listen_for_connection(CONNECTION_TYPE.ACTIVITY_INFO, new_connection);
|
75
|
}
|
76
|
|
77
|
/*
|
78
|
* EXPORTS_START
|
79
|
* EXPORT start_activity_info_server
|
80
|
* EXPORT report_script
|
81
|
* EXPORT report_settings
|
82
|
* EXPORTS_END
|
83
|
*/
|