-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
121 lines (112 loc) · 4.39 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
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
const BADGE_BACKGROUND_COLOR = "#008000";
const BADGE_TEXT_COLOR = "#FFFFFF";
// Deletes cookies for the given website
function deleteCookiesForWebsite(website) {
chrome.cookies.getAll({ domain: website }, function(cookies) {
for (let cookie of cookies) {
let url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain + cookie.path;
chrome.cookies.remove({ url: url, name: cookie.name });
}
});
}
// Deletes browsing history for the given website
function deleteHistoryForWebsite(website) {
chrome.history.search({ text: website }, function (results) {
results.forEach((result) => {
if (new URL(result.url).hostname === website) {
chrome.history.deleteUrl({ url: result.url });
}
});
});
}
// Sets the badge for the given tab
function setBadge(tab) {
if (!tab.url || tab.url === 'chrome://newtab/') {
return;
}
let website = new URL(tab.url).hostname;
if (!website || website.startsWith("chrome://")) {
chrome.action.setBadgeText({ text: "" });
return;
}
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[website] || { history: false, cache: false, cookies: false };
if (settings.history || settings.cache || settings.cookies) {
chrome.action.setBadgeBackgroundColor({ color: BADGE_BACKGROUND_COLOR });
chrome.action.setBadgeTextColor({ color: BADGE_TEXT_COLOR });
chrome.action.setBadgeText({ text: "ON" });
} else {
chrome.action.setBadgeText({ text: "" });
}
});
}
// Handles the cleanup for a website
function handleCleanup(website, settings) {
if (settings.history) {
deleteHistoryForWebsite(website);
}
if (settings.cookies) {
deleteCookiesForWebsite(website);
}
// Cache is handled in content.js
}
// Listener for tab updates
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === "complete") {
setBadge(tab);
if (tab.url) {
let website = new URL(tab.url).hostname;
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[website] || { history: false, cache: false, cookies: false };
handleCleanup(website, settings);
});
}
}
});
// Listener for tab activation
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
setBadge(tab);
});
});
// Listener for messages from popup.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'updateSettings') {
handleCleanup(request.hostname, request.settings);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
setBadge(tabs[0]);
});
}
});
// Listener for when a window is closed
chrome.windows.onRemoved.addListener(function (windowId) {
chrome.tabs.query({}, function (tabs) {
tabs.forEach(function (tab) {
if (tab.url) {
let website = new URL(tab.url).hostname;
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[website] || { history: false, cache: false, cookies: false };
handleCleanup(website, settings);
});
}
});
});
});
// Listener for when the browser is about to close
chrome.runtime.onSuspend.addListener(function() {
chrome.tabs.query({}, function(tabs) {
tabs.forEach(function(tab) {
if (tab.url) {
let website = new URL(tab.url).hostname;
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[website] || { history: false, cache: false, cookies: false };
handleCleanup(website, settings);
});
}
});
});
});