-
Notifications
You must be signed in to change notification settings - Fork 14
/
options.js
315 lines (275 loc) · 7.92 KB
/
options.js
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
/*
* Referer Modifier: Modify the Referer header in HTTP requests
* Copyright (C) 2017-2022 Fiona Klute
*
* This program 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.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
"use strict";
/* from i18n.js: */
/* global apply_i18n */
/* The domain row template */
var template = document.querySelector("#ref_row");
/*
* Preselect the given option in the <select> element.
*/
function selectOption(select, option)
{
for (let i = 0; i < select.options.length; i++)
{
if (option === select.options[i].value)
{
select.selectedIndex = i;
select.options[i].defaultSelected = true;
}
else
{
/* Ensure that other options are NOT selected */
select.options[i].defaultSelected = false;
}
}
}
/*
* Listener for "change" events on <select class="action"> elements,
* disables the matching "Referer" input field if the selected action
* does not use it.
*/
function actionSelectListener(event)
{
event.target.parentElement.parentElement
.querySelector(".referer").disabled =
(event.target.value != "replace");
}
/*
* Create a new domain row with the given content.
*/
function createDomainRow(hostname, origin, act, referer)
{
let node = document.importNode(template.content, true);
let host = node.querySelector(".hostname");
let origin_domain = node.querySelector(".origin-domain");
let action = node.querySelector(".action");
let ref = node.querySelector(".referer");
host.value = hostname;
origin_domain.value = origin ? origin : "";
ref.value = referer;
selectOption(action, act);
apply_i18n(node.firstElementChild);
let del = node.querySelector(".delete_row");
del.addEventListener(
"click",
function()
{
del.parentElement.parentElement.remove();
},
{ once: true });
let up = node.querySelector(".up_row");
up.addEventListener(
"click",
function()
{
let row = up.parentElement.parentElement;
let prev = row.previousElementSibling;
if (prev != null && prev.classList.contains("ref_entry"))
{
prev.before(row);
}
});
let down = node.querySelector(".down_row");
down.addEventListener(
"click",
function()
{
let row = up.parentElement.parentElement;
let next = row.nextElementSibling;
if (next != null && next.classList.contains("ref_entry"))
{
next.after(row);
}
});
return node;
}
/*
* Add an empty domain row.
*/
function addDomainRow()
{
let row = createDomainRow("", "", "replace", "");
let act = row.querySelector(".action");
act.addEventListener("change", actionSelectListener);
document.getElementById("hosts").appendChild(row);
}
/*
* Loads the configuration from storage. This function runs when the
* options page is loaded.
*/
function restoreOptions()
{
browser.storage.sync.get(["same", "any"]).then(
(result) =>
{
selectOption(document.querySelector("#any_action"),
result.any.action);
document.querySelector("#any_referer").value = result.any.referer;
selectOption(document.querySelector("#same_action"),
result.same.action);
document.querySelector("#same_referer")
.value = result.same.referer;
}, null);
browser.storage.sync.get({
domains: []
}).then((result) =>
{
console.debug("Refreshing domain config: " + JSON.stringify(result));
let hosts = document.getElementById("hosts");
/* Remove currently shown domain rows, if any */
let entries = hosts.getElementsByClassName("ref_entry");
while (entries.item(0) != null)
{
entries.item(0).remove();
}
if (result.domains.length == 0)
{
/* Add one empty row that will show the placeholders */
hosts.appendChild(createDomainRow("", "replace", ""));
}
else
{
/* Add currently configured domains */
for (let line of result.domains)
{
hosts.appendChild(createDomainRow(line.domain,
line.origin,
line.action,
line.referer));
}
}
/* Disable "Referer" field if the selected action does not use it. */
for (let act of hosts.getElementsByClassName("action"))
{
act.parentElement.parentElement
.querySelector(".referer")
.disabled = (act.value != "replace");
act.addEventListener("change", actionSelectListener);
}
});
}
/*
* Write the current content of the options page to storage.
*/
function saveOptions()
{
let entries = document.getElementsByClassName("ref_entry");
let domains = [];
for (let entry of entries)
{
let host = entry.querySelector(".hostname").value.trim();
let origin = entry.querySelector(".origin-domain").value.trim();
if (host.length == 0)
{
if (origin.length > 0)
{
window.alert(browser.i18n.getMessage(
"rule missing target", JSON.stringify(origin)));
}
continue;
}
let act = entry.querySelector(".action");
let rule = {
domain: host,
action: act.options[act.selectedIndex].value,
referer: entry.querySelector(".referer").value.trim()
};
if (origin !== "")
{
rule.origin = origin;
}
domains.push(rule);
}
let any_action = document.querySelector("#any_action");
let any_referer = document.querySelector("#any_referer");
let same_action = document.querySelector("#same_action");
let same_referer = document.querySelector("#same_referer");
browser.storage.sync.set({
domains: domains,
any: {
action: any_action.options[any_action.selectedIndex].value,
referer: any_referer.value
},
same: {
action: same_action.options[same_action.selectedIndex].value,
referer: same_referer.value
}
});
}
function exportConfig()
{
browser.storage.sync.get(["same", "any", "domains"]).then(
(result) =>
{
let exportConf = {
any: result.any,
same: result.same,
domains: result.domains
};
let blob = new Blob([JSON.stringify(exportConf, null, 2)],
{ type: "application/json" });
let url = URL.createObjectURL(blob);
let link = document.createElement("a");
link.href = url;
let date = new Date().toISOString().replaceAll(":", "_");
link.download = `referer_mod_config-${date}.json`;
link.dispatchEvent(new MouseEvent("click"));
}, null);
}
function enableImport()
{
document.getElementById("import_button").disabled = false;
document.getElementById("import_warn").hidden = false;
}
function importConfig()
{
let importFile = document.getElementById("import_file").files[0];
let reader = new FileReader();
reader.onload = function(e) { importConfigJSON(e.target.result); };
reader.readAsText(importFile);
}
function importConfigJSON(string)
{
/* TODO: catch errors during parse/store, check format */
let conf = JSON.parse(string);
console.debug(browser.i18n.getMessage("extensionName")
+ " imported configuration:" + JSON.stringify(conf));
browser.storage.sync.set({
domains: conf.domains,
any: conf.any,
same: conf.same
}).then(function()
{
restoreOptions();
}, null);
}
/* Load current configuration into form */
document.addEventListener("DOMContentLoaded", restoreOptions);
/* Enable save button */
document.getElementById("safe_conf").addEventListener("click", saveOptions);
/* Enable "Add domain" button */
document.getElementById("add_row").addEventListener("click", addDomainRow);
/* Enable configuration export button */
document.getElementById("export").addEventListener("click", exportConfig);
/* Enable import button when the user selects a file */
document.getElementById("import_file")
.addEventListener("change", enableImport);
/* Listener for configuration import */
document.getElementById("import_button")
.addEventListener("click", importConfig);