forked from airtower-luna/referer-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
271 lines (239 loc) · 7.08 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
/*
* Referer Modifier: Modify the Referer header in HTTP requests
* Copyright (C) 2017-2019 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";
/* Globals: The domain row template and its children */
var template = document.querySelector("#ref_row");
var host = template.content.querySelector(".hostname");
var action = template.content.querySelector(".action");
var ref = template.content.querySelector(".referer");
/*
* 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, act, referer)
{
host.value = hostname;
ref.value = referer;
selectOption(action, act);
return document.importNode(template.content, true);
}
/*
* Add an empty domain row and configure the delete button.
*/
function addDomainRow()
{
let row = createDomainRow("", "replace", "");
let b = row.querySelector(".delete_row");
b.addEventListener("click",
function()
{
b.parentElement.parentElement.remove();
},
{ once: true });
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.log("Refreshing domains: " + 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.action,
line.referer));
}
}
/* Enable delete buttons for domain rows */
for (let b of hosts.getElementsByClassName("delete_row"))
{
b.addEventListener("click",
function()
{
b.parentElement.parentElement.remove();
},
{ once: true });
}
/* 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();
if (host.length > 0)
{
let act = entry.querySelector(".action");
domains.push({
domain: host,
action: act.options[act.selectedIndex].value,
referer: entry.querySelector(".referer").value.trim()
});
}
}
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.querySelector("#export_blob");
link.href = url;
link.download = "referer_mod_config.json";
document.querySelector("#export_text").hidden = false;
}, 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.log(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);