-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
63 lines (56 loc) · 2 KB
/
Copy pathbackground.js
File metadata and controls
63 lines (56 loc) · 2 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
async function clearCookies() {
return await Promise.all([
browser.storage.local.get("domains"),
browser.cookies.getAll({}),
// this exists only for aesthetic reasons (popup window has time to show)
new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "testing");
})
])
.then(items => // namify the values returned by promises, process errors
{ return { domains: items[0].domains, cookies: items[1] }
},
(error => console.log(`Failed to read config or cookies: ${error}`)))
.then(items => {
var domains = items.domains;
var cookies = items.cookies;
var kept=0, removed=0;
var domainsArray = domains.split(",").map(d => d.trim());
for (var cookie of cookies) {
var deleteCookie = true;
for (var d of domainsArray) {
if (cookie.domain === d || cookie.domain.endsWith('.' + d)) {
// keep cookie
console.log("KEEPING " + cookie.domain + cookie.path + " --- " + cookie.name);
deleteCookie = false;
kept++;
break;
}
}
if (deleteCookie) {
var removingHttp = browser.cookies.remove({
url: "http://" + cookie.domain + cookie.path,
name: cookie.name
});
removingHttp.then(onCookieRemoved, onCookieError);
var removingHttps = browser.cookies.remove({
url: "https://" + cookie.domain + cookie.path,
name: cookie.name
});
removingHttps.then(onCookieRemoved, onCookieError);
console.log("REMOVING " + cookie.domain + cookie.path + " --- " + cookie.name);
removed++;
}
}
return {kept:kept, removed:removed};
});
}
function onCookieRemoved(cookie) {
try {
console.log("REMOVED " + cookie.domain + cookie.path + " --- " + cookie.name);
} catch (e) {}
return;
}
function onCookieError(error) {
console.log(`Error removing cookie: ${error}`);
}