-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.js
108 lines (89 loc) · 2.95 KB
/
cookies.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
const form = document.getElementById('control-row');
const go = document.getElementById('go');
const input = document.getElementById('input');
const message = document.getElementById('message');
// The async IIFE is necessary because Chrome <89 does not support top level await.
(async function initPopupWindow() {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab?.url) {
try {
let url = new URL(tab.url);
input.value = url.hostname;
} catch {
// ignore
}
}
input.focus();
})();
form.addEventListener('submit', handleFormSubmit);
async function handleFormSubmit(event) {
event.preventDefault();
clearMessage();
let url = stringToUrl(input.value);
if (!url) {
setMessage('Invalid URL');
return;
}
let message = await deleteDomainCookies(url.hostname);
setMessage(message);
}
function stringToUrl(input) {
// Start with treating the provided value as a URL
try {
return new URL(input);
} catch {
// ignore
}
// If that fails, try assuming the provided input is an HTTP host
try {
return new URL('http://' + input);
} catch {
// ignore
}
// If that fails ¯\_(ツ)_/¯
return null;
}
async function deleteDomainCookies(domain) {
let cookiesDeleted = 0;
try {
const cookies = await chrome.cookies.getAll({ domain });
if (cookies.length === 0) {
return 'No cookies found';
}
let pending = cookies.map(deleteCookie);
await Promise.all(pending);
cookiesDeleted = pending.length;
} catch (error) {
return `Unexpected error: ${error.message}`;
}
return `Deleted ${cookiesDeleted} cookie(s).`;
}
function deleteCookie(cookie) {
// Cookie deletion is largely modeled off of how deleting cookies works when using HTTP headers.
// Specific flags on the cookie object like `secure` or `hostOnly` are not exposed for deletion
// purposes. Instead, cookies are deleted by URL, name, and storeId. Unlike HTTP headers, though,
// we don't have to delete cookies by setting Max-Age=0; we have a method for that ;)
//
// To remove cookies set with a Secure attribute, we must provide the correct protocol in the
// details object's `url` property.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Secure
const protocol = cookie.secure ? 'https:' : 'http:';
// Note that the final URL may not be valid. The domain value for a standard cookie is prefixed
// with a period (invalid) while cookies that are set to `cookie.hostOnly == true` do not have
// this prefix (valid).
// https://developer.chrome.com/docs/extensions/reference/cookies/#type-Cookie
const cookieUrl = `${protocol}//${cookie.domain}${cookie.path}`;
return chrome.cookies.remove({
url: cookieUrl,
name: cookie.name,
storeId: cookie.storeId
});
}
function setMessage(str) {
message.textContent = str;
message.hidden = false;
}
function clearMessage() {
message.hidden = true;
message.textContent = '';
}