-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
179 lines (155 loc) · 6.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
/*
Copyright 2017 Craig Miskell
This file is part of CookieMaster, a Firefox Web Extension
CookieMaster 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.
CookieMaster 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 <http://www.gnu.org/licenses/>.
*/
function saveOptions() {
var allowListSelect = document.querySelector("#allowList");
var allowList = [];
for (var option of allowListSelect.options) {
allowList.push(option.value);
}
var ignoreSettingsWarning = document.querySelector("#ignoreSettingsWarning").checked
browser.storage.local.set({
thirdParty: document.querySelector("#thirdParty").value,
allowList: allowList,
ignoreSettingsWarning: ignoreSettingsWarning
});
browser.runtime.sendMessage({"type": MessageTypes.ConfigChanged});
}
function addDomainToDisplayList(domain) {
var allowListSelect = document.querySelector("#allowList");
var newOption = document.createElement("option");
newOption.value = domain;
newOption.text = domain;
for(var option of allowListSelect.options) {
if(option.value > domain) {
//This is the first option in the list which sorts lexically after the new one
// so insert immediately before the it
allowListSelect.add(newOption, option);
return;
}
}
//Didn't find any existing option to insert before; chuck it on the end
allowListSelect.add(newOption);
}
function displayOptions() {
getConfig().then((config) => {
document.querySelector("#thirdParty").value = config.thirdParty;
for (var domain of config.allowList) {
addDomainToDisplayList(domain);
}
document.querySelector("#ignoreSettingsWarning").checked = config.ignoreSettingsWarning;
});
}
function addSite(e) {
var allowListSelect = document.querySelector("#allowList");
var newSiteTextField = document.querySelector("#newSite");
var domain = newSiteTextField.value;
for(var option of allowListSelect.options) {
if(option.value == domain) {
return;
}
}
if(domain && (domain != "")) {
addDomainToDisplayList(domain);
newSiteTextField.value = "";
saveOptions();
}
}
function removeSites(e) {
var allowListSelect = document.querySelector("#allowList");
var selectedIndexes = []
for (var option of allowListSelect.selectedOptions) {
selectedIndexes.push(option.index);
}
//Reverse sort, so we're removing entries from the end first (not messing up the indexes of what remains)
selectedIndexes.sort(function(a, b) {
//Numerical comparison, not the default string; b-a => reverse sort
return b - a;
});
for(var index of selectedIndexes) {
allowListSelect.remove(index);
}
saveOptions();
}
var downloadIds = new Map();
function downloadsChanged(delta) {
if(delta.state && (delta.state.current=="complete")) {
if(downloadIds.has(delta.id)) {
URL.revokeObjectURL(downloadIds.get(delta.id));
downloadIds.delete(delta.id);
}
}
}
async function backupSettings(e) {
var config = await getConfig();
var json = [JSON.stringify(config)];
var blob = new Blob(json, {type : 'application/json'});
var url = URL.createObjectURL(blob);
var downloadId = await browser.downloads.download({
url: url,
filename: "cookie-master-settings.json",
saveAs: true
});
downloadIds.set(downloadId, url);
}
async function restoreSettings(e) {
var fileSelector = document.querySelector("#restoreFile");
var configText = await fileSelector.files[0].text();
var config = JSON.parse(configText);
// Not sure I like manipulating the UI and then saving, but it works.
// Trouble is, I don't want tweo places constructing the config object, so
// saveOptions is 'better'.
// It might be better in the session cookies branch?
document.querySelector("#thirdParty").value = config.thirdParty;
document.querySelector("#ignoreSettingsWarning").checked = config.ignoreSettingsWarning;
//Remove existing, add from uploaded config. I *really* don't like this, but...
var allowListSelect = document.querySelector("#allowList");
for(var i = allowListSelect.length; i >= 0 ; i--) {
allowListSelect.remove(i);
}
for(var domain of config.allowList) {
addDomainToDisplayList(domain);
}
saveOptions();
fileSelector.value = "";
}
// TODO: check the session-cookies branch logging commit to see why we changed to async here (is it necessary?)
function contentLoaded() {
displayOptions();
//Can't do this until the content is loaded, with the script in <head>. I like it there too
document.querySelector("#thirdParty").addEventListener("change", saveOptions);
document.querySelector("#ignoreSettingsWarning").addEventListener("change", saveOptions);
document.querySelector("#resetSettingsForm").addEventListener("submit", resetSettings);
document.querySelector("#backupSettings").addEventListener("click", backupSettings);
document.querySelector("#restoreSettings").addEventListener("click", restoreSettings);
document.querySelector("#removeSites").addEventListener("click", removeSites);
document.querySelector("#addSite").addEventListener("click", addSite);
document.getElementById('openLogs').href = browser.runtime.getURL("logs.html");
document.getElementById('helplink').href = browser.runtime.getURL("help.html");
}
function resetSettings() {
logger.warn("Clearing all settings");
browser.storage.local.clear();
resetToFactorySettings();
}
function handleMessage(message, sender, sendMessage) {
switch(message.type) {
case MessageTypes.MessageLogged:
displayLogs();
break;
}
}
document.addEventListener('DOMContentLoaded', contentLoaded);
browser.downloads.onChanged.addListener(downloadsChanged);
browser.runtime.onMessage.addListener(handleMessage);