-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
58 lines (51 loc) · 1.41 KB
/
background.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
function getDomainAndTLD(url) {
const urlObj = new URL(url);
const parts = urlObj.hostname.split(".");
if (parts.length > 2) {
return parts.slice(-2).join(".");
}
return urlObj.hostname;
}
chrome.action.onClicked.addListener((tab) => {
const url = new URL(tab.url);
const domain = getDomainAndTLD(tab.url);
// Clear cookies
chrome.cookies.getAll({ domain: domain }, (cookies) => {
for (let cookie of cookies) {
const cookieUrl = `http${cookie.secure ? "s" : ""}://${cookie.domain}${
cookie.path
}`;
chrome.cookies.remove({ url: cookieUrl, name: cookie.name });
}
});
// Clear local storage, session storage, and IndexedDB
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
function: clearStorages,
},
() => {
// Show a notification to the user
chrome.action.setBadgeText({ text: "Done", tabId: tab.id });
setTimeout(() => {
chrome.action.setBadgeText({ text: "", tabId: tab.id });
}, 2000);
}
);
});
function clearStorages() {
// Clear localStorage
localStorage.clear();
// Clear sessionStorage
sessionStorage.clear();
// Clear IndexedDB
if (window.indexedDB) {
window.indexedDB.databases().then((dbs) => {
dbs.forEach((db) => {
window.indexedDB.deleteDatabase(db.name);
});
});
}
// Force a page reload to ensure all changes take effect
location.reload();
}