1 |
01e977f9
|
Wojtek Kosior
|
/**
|
2 |
|
|
* This file is part of Haketilo.
|
3 |
|
|
*
|
4 |
|
|
* Function: Instantiate the Pattern Tree data structure, filled with mappings
|
5 |
|
|
* from IndexedDB.
|
6 |
|
|
*
|
7 |
07a883fe
|
Wojtek Kosior
|
* Copyright (C) 2021,2022 Wojtek Kosior
|
8 |
01e977f9
|
Wojtek Kosior
|
*
|
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 |
372d24ea
|
Wojtek Kosior
|
* license. Although I request that you do not make use of this code in a
|
42 |
01e977f9
|
Wojtek Kosior
|
* 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 |
702eefd2
|
Wojtek Kosior
|
#IF MOZILLA || MV3
|
49 |
01e977f9
|
Wojtek Kosior
|
#FROM common/browser.js IMPORT browser
|
50 |
702eefd2
|
Wojtek Kosior
|
#ENDIF
|
51 |
|
|
|
52 |
9d825eaa
|
Wojtek Kosior
|
let default_allow = {};
|
53 |
|
|
#EXPORT default_allow
|
54 |
|
|
|
55 |
702eefd2
|
Wojtek Kosior
|
let secret;
|
56 |
01e977f9
|
Wojtek Kosior
|
|
57 |
|
|
const tree = pqt.make();
|
58 |
|
|
#EXPORT tree
|
59 |
|
|
|
60 |
07a883fe
|
Wojtek Kosior
|
const currently_registered = new Map();
|
61 |
01e977f9
|
Wojtek Kosior
|
|
62 |
702eefd2
|
Wojtek Kosior
|
#IF MOZILLA || MV3
|
63 |
01e977f9
|
Wojtek Kosior
|
let registered_script = null;
|
64 |
|
|
let script_update_occuring = false;
|
65 |
|
|
let script_update_needed;
|
66 |
|
|
|
67 |
4c6a2323
|
Wojtek Kosior
|
async function update_content_script() {
|
68 |
01e977f9
|
Wojtek Kosior
|
if (script_update_occuring)
|
69 |
|
|
return;
|
70 |
|
|
|
71 |
|
|
script_update_occuring = true;
|
72 |
|
|
|
73 |
|
|
while (script_update_needed) {
|
74 |
|
|
script_update_needed = false;
|
75 |
|
|
|
76 |
|
|
const code = `\
|
77 |
9d825eaa
|
Wojtek Kosior
|
this.haketilo_secret = ${JSON.stringify(secret)};
|
78 |
|
|
this.haketilo_pattern_tree = ${JSON.stringify(tree)};
|
79 |
|
|
this.haketilo_default_allow = ${JSON.stringify(default_allow.value)};
|
80 |
01e977f9
|
Wojtek Kosior
|
if (this.haketilo_content_script_main)
|
81 |
|
|
haketilo_content_script_main();`;
|
82 |
|
|
|
83 |
|
|
const new_script = await browser.contentScripts.register({
|
84 |
|
|
"js": [{code}],
|
85 |
|
|
"matches": ["<all_urls>"],
|
86 |
|
|
"allFrames": true,
|
87 |
|
|
"runAt": "document_start"
|
88 |
|
|
});
|
89 |
|
|
|
90 |
|
|
if (registered_script)
|
91 |
|
|
registered_script.unregister();
|
92 |
|
|
|
93 |
|
|
registered_script = new_script;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
script_update_occuring = false;
|
97 |
|
|
}
|
98 |
07a883fe
|
Wojtek Kosior
|
#ENDIF
|
99 |
01e977f9
|
Wojtek Kosior
|
|
100 |
4c6a2323
|
Wojtek Kosior
|
function register(kind, object) {
|
101 |
07a883fe
|
Wojtek Kosior
|
if (kind === "mappings") {
|
102 |
|
|
for (const [pattern, resource] of Object.entries(object.payloads))
|
103 |
|
|
pqt.register(tree, pattern, object.identifier, resource);
|
104 |
|
|
} else /* if (kind === "blocking") */ {
|
105 |
|
|
/*
|
106 |
|
|
* All simple block/allow rules use "~allow" in place of mapping id.
|
107 |
|
|
* This way it won't collide with any real mapping id and will always
|
108 |
|
|
* be sorted as higher value than mapping ids.
|
109 |
|
|
*/
|
110 |
|
|
pqt.register(tree, object.pattern, "~allow", object.allow + 0);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
#IF MOZILLA || MV3
|
114 |
|
|
const id = kind === "mappings" ? object.identifier : object.pattern;
|
115 |
|
|
currently_registered.set(id, object);
|
116 |
702eefd2
|
Wojtek Kosior
|
#ENDIF
|
117 |
07a883fe
|
Wojtek Kosior
|
}
|
118 |
01e977f9
|
Wojtek Kosior
|
|
119 |
4c6a2323
|
Wojtek Kosior
|
function changed(kind, change) {
|
120 |
07a883fe
|
Wojtek Kosior
|
const old_version = currently_registered.get(change.key);
|
121 |
01e977f9
|
Wojtek Kosior
|
if (old_version !== undefined) {
|
122 |
07a883fe
|
Wojtek Kosior
|
if (kind === "mappings") {
|
123 |
|
|
for (const pattern in old_version.payloads)
|
124 |
|
|
pqt.deregister(tree, pattern, change.key);
|
125 |
|
|
} else /* if (kind === "blocking") */ {
|
126 |
|
|
pqt.deregister(tree, change.key, "~allow");
|
127 |
|
|
}
|
128 |
01e977f9
|
Wojtek Kosior
|
|
129 |
07a883fe
|
Wojtek Kosior
|
#IF MOZILLA || MV3
|
130 |
|
|
currently_registered.delete(change.key);
|
131 |
|
|
#ENDIF
|
132 |
01e977f9
|
Wojtek Kosior
|
}
|
133 |
|
|
|
134 |
|
|
if (change.new_val !== undefined)
|
135 |
07a883fe
|
Wojtek Kosior
|
register(kind, change.new_val);
|
136 |
01e977f9
|
Wojtek Kosior
|
|
137 |
702eefd2
|
Wojtek Kosior
|
#IF MOZILLA || MV3
|
138 |
01e977f9
|
Wojtek Kosior
|
script_update_needed = true;
|
139 |
|
|
setTimeout(update_content_script, 0);
|
140 |
702eefd2
|
Wojtek Kosior
|
#ENDIF
|
141 |
01e977f9
|
Wojtek Kosior
|
}
|
142 |
|
|
|
143 |
4c6a2323
|
Wojtek Kosior
|
function setting_changed(change) {
|
144 |
|
|
if (change.key !== "default_allow")
|
145 |
|
|
return;
|
146 |
|
|
|
147 |
|
|
default_allow.value = (change.new_val || {}).value;
|
148 |
|
|
|
149 |
|
|
#IF MOZILLA || MV3
|
150 |
|
|
script_update_needed = true;
|
151 |
|
|
setTimeout(update_content_script, 0);
|
152 |
|
|
#ENDIF
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
async function start(secret_) {
|
156 |
702eefd2
|
Wojtek Kosior
|
secret = secret_;
|
157 |
|
|
|
158 |
07a883fe
|
Wojtek Kosior
|
const [mapping_tracking, initial_mappings] =
|
159 |
7218849a
|
Wojtek Kosior
|
await haketilodb.track.mapping(ch => changed("mappings", ch));
|
160 |
07a883fe
|
Wojtek Kosior
|
const [blocking_tracking, initial_blocking] =
|
161 |
|
|
await haketilodb.track.blocking(ch => changed("blocking", ch));
|
162 |
|
|
|
163 |
|
|
initial_mappings.forEach(m => register("mappings", m));
|
164 |
|
|
initial_blocking.forEach(b => register("blocking", b));
|
165 |
01e977f9
|
Wojtek Kosior
|
|
166 |
9d825eaa
|
Wojtek Kosior
|
const [setting_tracking, initial_settings] =
|
167 |
4c6a2323
|
Wojtek Kosior
|
await haketilodb.track.settings(setting_changed);
|
168 |
|
|
|
169 |
9d825eaa
|
Wojtek Kosior
|
for (const setting of initial_settings) {
|
170 |
|
|
if (setting.name === "default_allow")
|
171 |
|
|
Object.assign(default_allow, setting);
|
172 |
|
|
}
|
173 |
|
|
|
174 |
702eefd2
|
Wojtek Kosior
|
#IF MOZILLA || MV3
|
175 |
01e977f9
|
Wojtek Kosior
|
script_update_needed = true;
|
176 |
|
|
await update_content_script();
|
177 |
702eefd2
|
Wojtek Kosior
|
#ENDIF
|
178 |
01e977f9
|
Wojtek Kosior
|
}
|
179 |
|
|
#EXPORT start
|