-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathsettings.js
More file actions
347 lines (319 loc) · 12 KB
/
Copy pathsettings.js
File metadata and controls
347 lines (319 loc) · 12 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* ClearURLs
* Copyright (c) 2017-2020 Kevin Röbert
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var settings = [];
const pickr = Pickr.create({
el: '#badged-color-picker',
theme: 'nano',
components: {
preview: true,
opacity: true,
hue: true,
default: '#FFA500',
comparison: false,
interaction: {
hex: true,
rgba: false,
hsla: false,
hsva: false,
cmyk: false,
input: true,
clear: false,
save: true
}
}
});
/**
* Load only when document is ready
*/
(function () {
pickr.on('init', () => {
getData();
setText();
document.getElementById('reset_settings_btn').onclick = reset;
document.getElementById('export_settings_btn').onclick = exportSettings;
document.getElementById('importSettings').onchange = importSettings;
document.getElementById('save_settings_btn').onclick = save;
});
})();
/**
* Reset everything.
* Set everthing to the default values.
*/
function reset() {
browser.runtime.sendMessage({
function: "initSettings",
params: []
}).then(handleResponse, handleError);
browser.runtime.sendMessage({
function: "saveOnExit",
params: []
}).then(handleResponse, handleError);
browser.runtime.sendMessage({
function: "reload",
params: []
}).then(handleResponse, handleError);
}
/**
* Saves the settings.
*/
function save() {
saveData("badged_color", pickr.getColor().toHEXA().toString())
.then(() => saveData("ruleURL", document.querySelector('input[name=ruleURL]').value))
.then(() => saveData("hashURL", document.querySelector('input[name=hashURL]').value))
.then(() => saveData(
"customRules",
document.querySelector('textarea[name=customRules]').value
), handleError)
.then(() => saveData("types", document.querySelector('input[name=types]').value))
.then(() => saveData("logLimit", Math.max(0, Math.min(5000, document.querySelector('input[name=logLimit]').value))))
.then(() => browser.runtime.sendMessage({
function: "setBadgedStatus",
params: []
}), handleError)
.then(() => browser.runtime.sendMessage({
function: "saveOnExit",
params: []
}), handleError)
.then(() => browser.runtime.sendMessage({
function: "reload",
params: []
}), handleError);
}
/**
* Translate a string with the i18n API.
*
* @param {string} string Name of the attribute used for localization
* @param {string[]} placeholders Array of placeholders
*/
function translate(string, ...placeholders) {
return browser.i18n.getMessage(string, placeholders);
}
/**
* Get the data.
*/
function getData() {
browser.runtime.sendMessage({
function: "getData",
params: ["badged_color"]
}).then(data => {
settings["badged_color"] = data.response;
pickr.setColor(data.response, false);
}).catch(handleError);
loadData("ruleURL")
.then(() => loadData("hashURL"))
.then(() => loadData("customRules"))
.then(() => loadData("types"))
.then(() => loadData("logLimit"))
.then(logData => {
if (logData.response === undefined) {
document.getElementById('logLimit_label').textContent = translate('setting_log_limit_label', "0");
} else {
document.getElementById('logLimit_label').textContent = translate('setting_log_limit_label', logData.response);
}
}).catch(handleError);
loadData("contextMenuEnabled")
.then(() => loadData("historyListenerEnabled"))
.then(() => loadData("localHostsSkipping"))
.then(() => loadData("referralMarketing"))
.then(() => loadData("domainBlocking"))
.then(() => loadData("pingBlocking"))
.then(() => loadData("eTagFiltering"))
.then(() => {
changeSwitchButton("localHostsSkipping", "localHostsSkipping");
changeSwitchButton("historyListenerEnabled", "historyListenerEnabled");
changeSwitchButton("contextMenuEnabled", "contextMenuEnabled");
changeSwitchButton("referralMarketing", "referralMarketing");
changeSwitchButton("domainBlocking", "domainBlocking");
changeSwitchButton("pingBlocking", "pingBlocking");
changeSwitchButton("eTagFiltering", "eTagFiltering");
}).catch(handleError);
}
/**
* Loads data from storage and saves into local variable.
*
* @param name data/variable name
* @returns {Promise<data>} requested data
*/
async function loadData(name) {
return new Promise((resolve, reject) => {
browser.runtime.sendMessage({
function: "getData",
params: [name]
}).then(data => {
settings[name] = data.response;
if (document.querySelector('*[id=' + name + ']') == null) {
console.debug(name)
}
switch (name) {
case "customRules":
document.querySelector('*[id=' + name + ']').value = JSON.stringify(data.response);
break;
default:
document.querySelector('*[id=' + name + ']').value = data.response;
}
resolve(data);
}, handleError);
});
}
/**
* Saves data to storage.
*
* @param key key of the data that should be saved
* @param data data that should be saved
* @returns {Promise<message>} message from background script
*/
async function saveData(key, data) {
return new Promise((resolve, reject) => {
browser.runtime.sendMessage({
function: "setData",
params: [key, data]
}).then(message => {
handleResponse(message);
resolve(message);
}, handleError);
});
}
/**
* Set the text for the UI.
*/
function setText() {
document.title = translate('settings_html_page_title');
document.getElementById('page_title').textContent = translate('settings_html_page_title');
document.getElementById('badged_color_label').textContent = translate('badged_color_label');
document.getElementById('reset_settings_btn').textContent = translate('setting_html_reset_button');
document.getElementById('reset_settings_btn').setAttribute('title', translate('setting_html_reset_button_title'));
document.getElementById('rule_url_label').textContent = translate('setting_rule_url_label');
document.getElementById('hash_url_label').textContent = translate('setting_hash_url_label');
document.getElementById('custom_rules_label').textContent = translate('setting_custom_rules_label');
document.getElementById('types_label').innerHTML = translate('setting_types_label');
document.getElementById('save_settings_btn').textContent = translate('settings_html_save_button');
document.getElementById('save_settings_btn').setAttribute('title', translate('settings_html_save_button_title'));
injectText("context_menu_enabled", "context_menu_enabled");
document.getElementById('history_listener_enabled').innerHTML = translate('history_listener_enabled');
injectText("local_hosts_skipping", "local_hosts_skipping");
document.getElementById('export_settings_btn_text').textContent = translate('setting_html_export_button');
document.getElementById('export_settings_btn').setAttribute('title', translate('setting_html_export_button_title'));
document.getElementById('import_settings_btn_text').textContent = translate('setting_html_import_button');
document.getElementById('importSettings').setAttribute('title', translate('setting_html_import_button_title'));
injectText("referral_marketing_enabled", "referral_marketing_enabled");
injectText("domain_blocking_enabled", "domain_blocking_enabled");
document.getElementById('ping_blocking_enabled').innerHTML = translate('ping_blocking_enabled');
document.getElementById('ping_blocking_enabled').setAttribute('title', translate('ping_blocking_enabled_title'));
document.getElementById('eTag_filtering_enabled').innerHTML = translate('eTag_filtering_enabled');
document.getElementById('eTag_filtering_enabled').setAttribute('title', translate('eTag_filtering_enabled_title'));
}
/**
* This function exports all ClearURLs settings with statistics and rules.
*/
function exportSettings() {
browser.runtime.sendMessage({
function: "storageAsJSON",
params: []
}).then((data) => {
let blob = new Blob([JSON.stringify(data.response)], {type: 'application/json'});
browser.downloads.download({
'url': URL.createObjectURL(blob),
'filename': 'ClearURLs.conf',
'saveAs': true
}).catch(handleError);
}).catch(handleError);
}
/**
* This function imports an exported ClearURLs setting and overwrites the old one.
*/
function importSettings(evt) {
let file = evt.target.files[0];
let fileReader = new FileReader();
fileReader.onload = function (e) {
let data = JSON.parse(e.target.result);
const length = Object.keys(data).length;
let i = 0;
Object.entries(data).forEach(([key, value]) => {
browser.runtime.sendMessage({
function: "setData",
params: [key, value]
}).then(() => {
i++;
if (i === length) {
location.reload();
}
}, handleError);
});
};
fileReader.readAsText(file);
}
function handleResponse(message) {
console.log(`Message from the background script: ${message.response}`);
}
function handleError(error) {
console.log(`Error: ${error}`);
}
/**
* Change the value of a switch button.
* @param {string} id HTML id
* @param {string} storageID storage internal id
*/
function changeSwitchButton(id, storageID) {
let element = document.getElementById(id);
element.onchange = function () {
browser.runtime.sendMessage({
function: "setData",
params: [storageID, element.checked]
}).then(() => {
if (storageID === "globalStatus") {
browser.runtime.sendMessage({
function: "changeIcon",
params: []
}).catch(handleError);
}
browser.runtime.sendMessage({
function: "saveOnExit",
params: []
}).catch(handleError);
}).catch(handleError);
};
setSwitchButton(id, storageID);
}
/**
* Helper function to inject the translated text and tooltip.
*
* @param {string} id ID of the HTML element
* @param {string} attribute Name of the attribute used for localization
* @param {string} tooltip
*/
function injectText(id, attribute, tooltip = "") {
let object = document.getElementById(id);
object.textContent = translate(attribute);
/*
This function will throw an error if no translation
is found for the tooltip. This is a planned error.
*/
tooltip = translate(attribute + "_title");
if (tooltip !== "") {
object.setAttribute('title', tooltip);
}
}
/**
* Set the value of a switch button.
* @param {string} id HTML id
* @param {string} varname js internal variable name
*/
function setSwitchButton(id, varname) {
let element = document.getElementById(id);
element.checked = settings[varname];
}