-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_worker.js
More file actions
95 lines (80 loc) · 2.65 KB
/
service_worker.js
File metadata and controls
95 lines (80 loc) · 2.65 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
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
// Auto Wipe - Manifest V3 service worker
// Wipes selected browsing data on selected triggers, plus on-demand via popup.
const DEFAULT_SETTINGS = {
wipeHistory: true,
wipeCache: true,
wipeDownloads: true,
triggerStartup: true,
triggerLastWindowClose: true
};
async function getSettings() {
const stored = await chrome.storage.sync.get(DEFAULT_SETTINGS);
return { ...DEFAULT_SETTINGS, ...stored };
}
function buildDataToRemove(settings) {
const data = {};
if (settings.wipeHistory) data.history = true;
if (settings.wipeDownloads) data.downloads = true;
if (settings.wipeCache) {
data.cache = true;
data.cacheStorage = true;
}
return data;
}
async function wipeSelected(reason) {
const settings = await getSettings();
const dataToRemove = buildDataToRemove(settings);
if (!Object.keys(dataToRemove).length) {
console.log(`[AutoWipe] Nothing selected to wipe (${reason}).`);
return { ok: true, reason, wiped: [] };
}
await chrome.browsingData.remove({ since: 0 }, dataToRemove);
console.log(`[AutoWipe] Wipe completed (${reason}).`, dataToRemove);
return { ok: true, reason, wiped: Object.keys(dataToRemove) };
}
// Initialize defaults on install (only if not already set)
chrome.runtime.onInstalled.addListener(async () => {
const current = await chrome.storage.sync.get(null);
if (!current || Object.keys(current).length === 0) {
await chrome.storage.sync.set(DEFAULT_SETTINGS);
}
});
// Trigger: browser startup
chrome.runtime.onStartup.addListener(async () => {
const settings = await getSettings();
if (settings.triggerStartup) {
try {
await wipeSelected("onStartup");
} catch (err) {
console.warn("[AutoWipe] Wipe failed (onStartup):", err);
}
}
});
// Trigger: best-effort when last window closes
chrome.windows.onRemoved.addListener(async () => {
const settings = await getSettings();
if (!settings.triggerLastWindowClose) return;
try {
const remaining = await chrome.windows.getAll();
if (!remaining || remaining.length === 0) {
await wipeSelected("lastWindowClosed");
}
} catch (err) {
console.warn("[AutoWipe] Failed checking remaining windows:", err);
}
});
// On-demand wipe triggered from the popup
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg && msg.type === "WIPE_NOW") {
(async () => {
try {
const result = await wipeSelected("wipeNow");
sendResponse(result);
} catch (err) {
console.warn("[AutoWipe] Wipe failed (wipeNow):", err);
sendResponse({ ok: false, error: String(err) });
}
})();
return true; // keep the message channel open for async response
}
});