-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
190 lines (171 loc) · 5.86 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const browser = typeof chrome != 'undefined' ? chrome : browser;
const browserNamespace = typeof chrome != 'undefined' ? 'chrome' : 'browser';
const contextMenus = browser.contextMenus;
let activeTabId = null;
function getDomain(url) {
if (url instanceof URL) {
return url.hostname;
} else {
return new URL(url.toString()).hostname;
}
}
function clearCookies(url, cookies, callback) {
let deleteCount = 0;
function removeCallback() {
if (++deleteCount >= cookies.length) {
if (callback) {
callback(deleteCount);
}
}
}
if (!cookies || cookies.length == 0) {
callback(0);
} else {
for (const cookie of cookies) {
console.debug('Removing cookie:', cookie);
browser.cookies.remove({
url,
name: cookie.name,
storeId: cookie.storeId
}, removeCallback);
}
}
}
function clearCookiesWithUrl(url, callback) {
browser.cookies.getAll({url}, cookies => {
clearCookies(url, cookies, callback);
});
}
function clearLocalStorage(tab, callback) {
browser.tabs.executeScript(tab.id, {code: 'localStorage.clear()'}, callback);
}
function clearSessionStorage(tab, callback) {
browser.tabs.executeScript(tab.id, {code: 'sessionStorage.clear()'}, callback);
}
function updateMenusInternal(activeTab) {
// URL may be blank if user switched to a chrome:// tab or something like that.
const hasUrl = !!activeTab && !!activeTab.url;
const siteName = hasUrl ? getDomain(activeTab.url) : 'this site';
const isMenuEnabled = hasUrl;
contextMenus.update('clearCookies', {
title: 'Clear cookies for ' + siteName,
enabled: isMenuEnabled
});
contextMenus.update('clearCookiesAndReload', {enabled: isMenuEnabled});
contextMenus.update('clearLocalStorage', {
title: 'Clear local storage for ' + siteName,
enabled: isMenuEnabled
});
contextMenus.update('clearLocalStorageAndReload', {enabled: isMenuEnabled});
contextMenus.update('clearSessionStorage', {
title: 'Clear session storage for ' + siteName,
enabled: isMenuEnabled
});
contextMenus.update('clearSessionStorageAndReload', {enabled: isMenuEnabled});
}
function updateMenus(tab) {
console.log('Updating menus for tab:', tab);
if (tab != null && tab.active) {
updateMenusInternal(tab);
} else if (activeTabId != null) {
browser.tabs.get(activeTabId, tab => {
updateMenusInternal(tab);
});
}
}
contextMenus.onClicked.addListener((info, tab) => {
console.debug('Context menu item was clicked:', info.menuItemId);
if (!tab.active) {
console.debug('No active tab found, ignoring context menu item click');
return;
}
console.log('Current tab URL:', tab.url);
let reload = false;
switch (info.menuItemId) {
case 'clearCookiesAndReload':
reload = true;
case 'clearCookies':
try {
clearCookiesWithUrl(tab.url, deletedCount => {
if (browser.runtime.lastError) {
console.error('Error deleting cookies for ' + tab.url + ': ' + e.message);
} else if (reload) {
if (deletedCount > 0) {
browser.tabs.reload(tab.id);
}
}
});
} catch (e) {
console.error('Error deleting cookies for ' + tab.url + ': ' + e.message);
}
break;
case 'clearLocalStorageAndReload':
reload = true;
case 'clearLocalStorage':
try {
clearLocalStorage(tab, () => {
if (browser.runtime.lastError) {
console.error('Error clearing local storage: ' + e.message);
} else if (reload) {
browser.tabs.reload(tab.id);
}
});
} catch (e) {
console.error('Error clearing local storage: ' + e.message);
}
break;
case 'clearSessionStorageAndReload':
reload = true;
case 'clearSessionStorage':
try {
clearSessionStorage(tab, () => {
if (browser.runtime.lastError) {
console.error('Error clearing session storage: ' + e.message);
} else if (reload) {
browser.tabs.reload(tab.id);
}
});
} catch (e) {
console.error('Error clearing session storage: ' + e.message);
}
break;
}
});
contextMenus.removeAll();
contextMenus.create({
id: 'clearCookies',
title: 'Clear cookies for this site',
contexts: ['browser_action', 'page', 'page_action']
});
contextMenus.create({
id: 'clearCookiesAndReload',
title: 'Clear cookies and reload',
contexts: ['browser_action', 'page', 'page_action']
});
contextMenus.create({
id: 'clearLocalStorage',
title: 'Clear local storage',
contexts: ['browser_action', 'page', 'page_action']
});
contextMenus.create({
id: 'clearLocalStorageAndReload',
title: 'Clear local storage and reload',
contexts: ['browser_action', 'page', 'page_action']
});
contextMenus.create({
id: 'clearSessionStorage',
title: 'Clear session storage',
contexts: ['browser_action', 'page', 'page_action']
});
contextMenus.create({
id: 'clearSessionStorageAndReload',
title: 'Clear session storage and reload',
contexts: ['browser_action', 'page', 'page_action']
});
browser.tabs.onActivated.addListener(activeInfo => {
activeTabId = activeInfo.tabId;
updateMenus();
});
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
updateMenus(tab);
});