1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/**
* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
*
* Copyright (C) 2017 Nathan Nichols
* Copyright (C) 2018 Giorgio maone
*
* This file is part of GNU LibreJS.
*
* GNU LibreJS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GNU LibreJS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU LibreJS. If not, see <http://www.gnu.org/licenses/>.
*/
(() => {
"use strict";
const LIST_NAMES = ["white", "black"];
var Model = {
lists: {},
prefs: null,
malformedUrl(url) {
let error = null;
try {
let objUrl = new URL(url);
url = objUrl.href;
if (!objUrl.protocol.startsWith("http")) {
error = "Please enter http:// or https:// URLs only";
} else if (!/^[^*]+\*?$/.test(url)) {
error = "Only one single trailing path wildcard (/*) allowed";
}
} catch (e) {
if (/^https?:\/\/\*\./.test(url)) {
return this.malformedUrl(url.replace("*.", ""));
}
error = "Invalid URL";
if (url && !url.includes("://")) error += ": missing protocol, either http:// or https://";
else if (url.endsWith("://")) error += ": missing domain name";
}
return error;
},
async save(prefs = this.prefs) {
if (prefs !== this.prefs) {
this.prefs = Object.assign(this.prefs, prefs);
}
this.saving = true;
try {
return await browser.storage.local.set(prefs);
} finally {
this.saving = false;
}
},
async addToList(list, ...items) {
let other = list === Model.lists.black ? Model.lists.white : Model.lists.black;
this.saving = true;
try {
await Promise.all([
other.remove(...items),
list.store(...items)
]);
} finally {
this.saving = false;
}
}
};
Model.loading = (async () => {
let prefsNames = [
"whitelist",
"blacklist",
"subject",
"body"
];
Model.prefs = await browser.storage.local.get(prefsNames.map(name => `pref_${name}`));
for (let listName of LIST_NAMES) {
let prefName = `pref_${listName}list`;
await (Model.lists[listName] = new ListStore(prefName, Storage.CSV))
.load(Model.prefs[prefName]);
}
})();
var Controller = {
init() {
let widgetsRoot = this.root = document.getElementById("widgets");
for (let widget of widgetsRoot.querySelectorAll('[id^="pref_"]')) {
if (widget.id in Model.lists) {
populateListUI(widget);
} else if (widget.id in Model.prefs) {
widget.value = Model.prefs[widget.id];
}
}
this.populateListUI();
this.syncAll();
for (let ev in Listeners) {
widgetsRoot.addEventListener(ev, Listeners[ev]);
}
document.getElementById("site").onfocus = e => {
if (!e.target.value.trim()) {
e.target.value = "https://";
}
};
browser.storage.onChanged.addListener(changes => {
if (!Model.saving &&
("pref_whitelist" in changes || "pref_blacklist" in changes)) {
setTimeout(() => {
this.populateListUI();
this.syncAll();
}, 10);
}
});
},
async addSite(list) {
let url = document.getElementById("site").value.trim();
if (url && !Model.malformedUrl(url)) {
await this.addToList(list, url);
}
},
async addToList(list, ...items) {
await Model.addToList(list, ...items);
this.populateListUI();
this.syncAll();
},
async swapSelection(list) {
let origin = list === Model.lists.black ? "white" : "black";
await this.addToList(list, ...Array.prototype.map.call(
document.querySelectorAll(`select#${origin} option:checked`),
option => option.value)
);
},
syncAll() {
this.syncListsUI();
this.syncSiteUI();
},
syncSiteUI() {
let widget = document.getElementById("site");
let list2button = listName => document.getElementById(`cmd-${listName}list-site`);
for (let bi of LIST_NAMES.map(list2button)) {
bi.disabled = true;
}
let url = widget.value.trim();
let malformedUrl = url && Model.malformedUrl(url);
widget.classList.toggle("error", !!malformedUrl);
document.getElementById("site-error").textContent = malformedUrl || "";
if (!url) return;
if (url !== widget.value) {
widget.value = url;
}
for (let listName of LIST_NAMES) {
let list = Model.lists[listName];
if (!list.contains(url)) {
list2button(listName).disabled = false;
}
}
},
syncListsUI() {
let total = 0;
for (let id of ["black", "white"]) {
let selected = document.querySelectorAll(`select#${id} option:checked`).length;
let other = id === "black" ? "white" : "black";
document.getElementById(`cmd-${other}list`).disabled = selected === 0;
total += selected;
}
document.getElementById("cmd-delete").disabled = total === 0;
},
async deleteSelection() {
for (let id of ["black", "white"]) {
let selection = document.querySelectorAll(`select#${id} option:checked`);
await Model.lists[id].remove(...Array.prototype.map.call(selection, option => option.value));
}
this.populateListUI();
this.syncAll();
},
populateListUI(widget) {
if (!widget) {
for(let id of ["white", "black"]) {
this.populateListUI(document.getElementById(id));
}
return;
}
widget.innerHTML = "";
let items = [...Model.lists[widget.id].items].sort();
let options = new DocumentFragment();
for (let item of items) {
let option = document.createElement("option");
option.value = option.textContent = option.title = item;
options.appendChild(option);
}
widget.appendChild(options);
}
};
var KeyEvents = {
Delete(e) {
if (e.target.matches("#lists select")) {
Controller.deleteSelection();
}
},
Enter(e) {
if (e.target.id === "site") {
e.target.parentElement.querySelector("button[default]").click();
}
},
KeyA(e) {
if (e.target.matches("select") && e.ctrlKey) {
for (let o of e.target.options) {
o.selected = true;
}
Controller.syncListsUI();
}
}
}
var Listeners = {
async change(e) {
let {target} = e;
let {id} = target;
if (id in Model.lists) {
Controller.syncListsUI();
let selection = target.querySelectorAll("option:checked");
if (selection.length === 1) {
document.getElementById("site").value = selection[0].value;
}
return;
}
},
click(e) {
let {target} = e;
if (!/^cmd-(white|black|delete)(list-site)?/.test(target.id)) return;
e.preventDefault();
let cmd = RegExp.$1;
if (cmd === "delete") {
Controller.deleteSelection();
return;
}
let list = Model.lists[cmd];
if (list) {
Controller[RegExp.$2 ? "addSite" : "swapSelection"](list);
return;
}
},
keypress(e) {
let {code} = e;
if (code && typeof KeyEvents[code] === "function") {
if (KeyEvents[code](e) === false) {
e.preventDefault();
}
return;
}
},
async input(e) {
let {target} = e;
let {id} = target;
if (!id) return;
if (id === "site") {
Controller.syncSiteUI();
let url = target.value;
if (url) {
let o = document.querySelector(`#lists select option[value="${url}"]`);
if (o) {
o.scrollIntoView();
o.selected = true;
}
}
return;
}
if (id.startsWith("pref_")) {
await Model.save({[id]: target.value});
return;
}
}
};
window.addEventListener("DOMContentLoaded", async e => {
await Model.loading;
Controller.init();
});
})();
|