-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
138 lines (115 loc) · 3.88 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
/*
Firefox addon "Cookie Whitelist"
Copyright (C) 2023 Manuel Reimer <[email protected]>
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 <http://www.gnu.org/licenses/>.
*/
const get = (id) => document.getElementById(id);
async function CheckboxChanged(e) {
if (e.target.id.match(/([a-z_]+)_checkbox/)) {
let pref = RegExp.$1;
let params = {};
params[pref] = e.target.checked;
await Storage.set(params);
await browser.runtime.sendMessage({type: "OptionsChanged"});
}
}
async function WLCheckboxChanged(e) {
const checkbox = e.target;
const tr = checkbox.parentNode.parentNode;
const domain = tr.childNodes[1].innerText;
const prefs = await Storage.get();
const whitelist = prefs.whitelist;
if (checkbox.checked) {
tr.classList.add("whitelisted");
whitelist[domain] = true;
}
else {
tr.classList.remove("whitelisted");
delete whitelist[domain];
}
await Storage.set({"whitelist": whitelist});
}
async function FillDomainList() {
// Memory for the domain data
const domains = [];
const counter = {};
// Get list of domains which have cookies stored
const cookies = await browser.cookies.getAll({"partitionKey": {}});
cookies.forEach((cookie) => {
if (domains.includes(cookie.domain))
counter[cookie.domain]++;
else {
domains.push(cookie.domain);
counter[cookie.domain] = 1;
}
});
// Integrate whitelisted domains even if they currently have no cookies
const prefs = await Storage.get();
const whitelist = prefs.whitelist;
Object.keys(whitelist).forEach((domain) => {
if (!domains.includes(domain)) {
domains.push(domain);
counter[domain] = 0;
}
});
// Sort the list.
domains.sort((a, b) => {
const regex = /^(.*?)\.?([^\.]+)\.([^\.]+)$/;
partsa = a.match(regex);
partsb = b.match(regex);
if (partsa && partsb) {
a = `${partsa[2]}.${partsa[3]}.${partsa[1]}`;
b = `${partsb[2]}.${partsb[3]}.${partsb[1]}`;
}
else
console.log("Error ", a, b);
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
});
// Generate the domain list.
const tbody = get("domains_body");
domains.forEach((domain) => {
const tr = tbody.appendChild(document.createElement("tr"));
const check = document.createElement("input");
check.type = "checkbox";
check.addEventListener("change", WLCheckboxChanged);
tr.appendChild(document.createElement("td")).appendChild(check);
tr.appendChild(document.createElement("td")).innerText = domain;
tr.appendChild(document.createElement("td")).innerText = counter[domain];
if (whitelist.hasOwnProperty(domain)) {
tr.classList.add("whitelisted");
check.checked = true;
}
});
console.log(whitelist);
}
function init() {
loadOptions();
FillDomainList();
get("clearcookies_checkbox").addEventListener("change", CheckboxChanged);
get("clearstorage_checkbox").addEventListener("change", CheckboxChanged);
}
function loadOptions() {
Storage.get().then((result) => {
get("clearcookies_checkbox").checked = result.clearcookies;
get("clearstorage_checkbox").checked = result.clearstorage;
});
}
// Register event listener to receive option update notifications
browser.runtime.onMessage.addListener((data, sender) => {
if (data.type == "OptionsChanged")
loadOptions();
});
init();