1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Instantiate the Pattern Tree data structure, filled with mappings
|
5
|
* from IndexedDB.
|
6
|
*
|
7
|
* Copyright (C) 2021,2022 Wojtek Kosior
|
8
|
*
|
9
|
* This program is free software: you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation, either version 3 of the License, or
|
12
|
* (at your option) any later version.
|
13
|
*
|
14
|
* This program is distributed in the hope that it will be useful,
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
* GNU General Public License for more details.
|
18
|
*
|
19
|
* As additional permission under GNU GPL version 3 section 7, you
|
20
|
* may distribute forms of that code without the copy of the GNU
|
21
|
* GPL normally required by section 4, provided you include this
|
22
|
* license notice and, in case of non-source distribution, a URL
|
23
|
* through which recipients can access the Corresponding Source.
|
24
|
* If you modify file(s) with this exception, you may extend this
|
25
|
* exception to your version of the file(s), but you are not
|
26
|
* obligated to do so. If you do not wish to do so, delete this
|
27
|
* exception statement from your version.
|
28
|
*
|
29
|
* As a special exception to the GPL, any HTML file which merely
|
30
|
* makes function calls to this code, and for that purpose
|
31
|
* includes it by reference shall be deemed a separate work for
|
32
|
* copyright law purposes. If you modify this code, you may extend
|
33
|
* this exception to your version of the code, but you are not
|
34
|
* obligated to do so. If you do not wish to do so, delete this
|
35
|
* exception statement from your version.
|
36
|
*
|
37
|
* You should have received a copy of the GNU General Public License
|
38
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
39
|
*
|
40
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
41
|
* license. Although I request that you do not make use of this code in a
|
42
|
* proprietary program, I am not going to enforce this in court.
|
43
|
*/
|
44
|
|
45
|
#IMPORT common/patterns_query_tree.js AS pqt
|
46
|
#IMPORT common/indexeddb.js AS haketilodb
|
47
|
|
48
|
#IF MOZILLA || MV3
|
49
|
#FROM common/browser.js IMPORT browser
|
50
|
#ENDIF
|
51
|
|
52
|
let default_allow = {};
|
53
|
#EXPORT default_allow
|
54
|
|
55
|
let secret;
|
56
|
|
57
|
const tree = pqt.make();
|
58
|
#EXPORT tree
|
59
|
|
60
|
const currently_registered = new Map();
|
61
|
|
62
|
#IF MOZILLA || MV3
|
63
|
let registered_script = null;
|
64
|
let script_update_occuring = false;
|
65
|
let script_update_needed;
|
66
|
|
67
|
async function update_content_script()
|
68
|
{
|
69
|
if (script_update_occuring)
|
70
|
return;
|
71
|
|
72
|
script_update_occuring = true;
|
73
|
|
74
|
while (script_update_needed) {
|
75
|
script_update_needed = false;
|
76
|
|
77
|
const code = `\
|
78
|
this.haketilo_secret = ${JSON.stringify(secret)};
|
79
|
this.haketilo_pattern_tree = ${JSON.stringify(tree)};
|
80
|
this.haketilo_default_allow = ${JSON.stringify(default_allow.value)};
|
81
|
if (this.haketilo_content_script_main)
|
82
|
haketilo_content_script_main();`;
|
83
|
|
84
|
const new_script = await browser.contentScripts.register({
|
85
|
"js": [{code}],
|
86
|
"matches": ["<all_urls>"],
|
87
|
"allFrames": true,
|
88
|
"runAt": "document_start"
|
89
|
});
|
90
|
|
91
|
if (registered_script)
|
92
|
registered_script.unregister();
|
93
|
|
94
|
registered_script = new_script;
|
95
|
}
|
96
|
|
97
|
script_update_occuring = false;
|
98
|
}
|
99
|
#ENDIF
|
100
|
|
101
|
function register(kind, object)
|
102
|
{
|
103
|
if (kind === "mappings") {
|
104
|
for (const [pattern, resource] of Object.entries(object.payloads))
|
105
|
pqt.register(tree, pattern, object.identifier, resource);
|
106
|
} else /* if (kind === "blocking") */ {
|
107
|
/*
|
108
|
* All simple block/allow rules use "~allow" in place of mapping id.
|
109
|
* This way it won't collide with any real mapping id and will always
|
110
|
* be sorted as higher value than mapping ids.
|
111
|
*/
|
112
|
pqt.register(tree, object.pattern, "~allow", object.allow + 0);
|
113
|
}
|
114
|
|
115
|
#IF MOZILLA || MV3
|
116
|
const id = kind === "mappings" ? object.identifier : object.pattern;
|
117
|
currently_registered.set(id, object);
|
118
|
#ENDIF
|
119
|
}
|
120
|
|
121
|
function changed(kind, change)
|
122
|
{
|
123
|
const old_version = currently_registered.get(change.key);
|
124
|
if (old_version !== undefined) {
|
125
|
if (kind === "mappings") {
|
126
|
for (const pattern in old_version.payloads)
|
127
|
pqt.deregister(tree, pattern, change.key);
|
128
|
} else /* if (kind === "blocking") */ {
|
129
|
pqt.deregister(tree, change.key, "~allow");
|
130
|
}
|
131
|
|
132
|
#IF MOZILLA || MV3
|
133
|
currently_registered.delete(change.key);
|
134
|
#ENDIF
|
135
|
}
|
136
|
|
137
|
if (change.new_val !== undefined)
|
138
|
register(kind, change.new_val);
|
139
|
|
140
|
#IF MOZILLA || MV3
|
141
|
script_update_needed = true;
|
142
|
setTimeout(update_content_script, 0);
|
143
|
#ENDIF
|
144
|
}
|
145
|
|
146
|
async function start(secret_)
|
147
|
{
|
148
|
secret = secret_;
|
149
|
|
150
|
const [mapping_tracking, initial_mappings] =
|
151
|
await haketilodb.track.mapping(ch => changed("mappings", ch));
|
152
|
const [blocking_tracking, initial_blocking] =
|
153
|
await haketilodb.track.blocking(ch => changed("blocking", ch));
|
154
|
|
155
|
initial_mappings.forEach(m => register("mappings", m));
|
156
|
initial_blocking.forEach(b => register("blocking", b));
|
157
|
|
158
|
const set_allow_val = ch => default_allow.value = (ch.new_val || {}).value;
|
159
|
const [setting_tracking, initial_settings] =
|
160
|
await haketilodb.track.settings(set_allow_val);
|
161
|
for (const setting of initial_settings) {
|
162
|
if (setting.name === "default_allow")
|
163
|
Object.assign(default_allow, setting);
|
164
|
}
|
165
|
|
166
|
#IF MOZILLA || MV3
|
167
|
script_update_needed = true;
|
168
|
await update_content_script();
|
169
|
#ENDIF
|
170
|
}
|
171
|
#EXPORT start
|