forked from rNeomy/auto-tab-discard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
327 lines (319 loc) · 9.78 KB
/
menu.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* globals discard, query, notify, navigate, starters, storage, prefs, isFirefox, number, interrupts, inprogress */
'use strict';
// Context Menu
{
const TST = '[email protected]';
const onStartup = async () => {
const contexts = ['browser_action'];
if (chrome.contextMenus.ContextType.TAB && prefs['tab.context']) {
contexts.push('tab');
}
if (prefs['page.context']) {
contexts.push('page');
}
const create = arr => {
chrome.contextMenus.removeAll(() => {
arr.forEach(o => chrome.contextMenus.create(o));
});
};
create([{
id: 'discard-tab',
title: chrome.i18n.getMessage('menu_discard_tab'),
contexts,
documentUrlPatterns: ['*://*/*']
},
{
id: 'discard-tree',
title: chrome.i18n.getMessage('menu_discard_tree'),
contexts,
documentUrlPatterns: ['*://*/*']
},
{
id: 'discard-other-windows',
title: chrome.i18n.getMessage('menu_discard_other_windows'),
contexts
},
{
id: 'discard-sub-menu',
title: chrome.i18n.getMessage('menu_discard_menu'),
contexts
},
{
id: 'discard-tabs',
title: chrome.i18n.getMessage('menu_discard_tabs'),
contexts
},
{
id: 'discard-window',
title: chrome.i18n.getMessage('menu_discard_window'),
contexts,
parentId: 'discard-sub-menu'
},
{
id: 'discard-rights',
title: chrome.i18n.getMessage('menu_discard_rights'),
contexts,
parentId: 'discard-sub-menu'
},
{
id: 'discard-lefts',
title: chrome.i18n.getMessage('menu_discard_lefts'),
contexts,
parentId: 'discard-sub-menu'
},
{
id: 'auto-discardable',
title: chrome.i18n.getMessage('popup_allowed'),
contexts,
documentUrlPatterns: ['*://*/*']
},
{
id: 'whitelist-domain',
title: chrome.i18n.getMessage('menu_whitelist_domain'),
contexts,
documentUrlPatterns: ['*://*/*']
},
prefs['link.context'] ? {
id: 'open-tab-then-discard',
title: chrome.i18n.getMessage('menu_open_tab_then_discard'),
contexts: ['link', 'bookmark'].filter(a => chrome.contextMenus.ContextType[a.toUpperCase()]),
documentUrlPatterns: ['*://*/*']
} : null].filter(o => o));
};
starters.push(onStartup);
const onClicked = async (info, tab) => {
if (tab && !tab.url) { // Tree Style Tab 3.0.12 and later don't deliver a real tab.
// eslint-disable-next-line require-atomic-updates
tab = await new Promise(resolve => chrome.tabs.get(tab.id, resolve));
}
if (typeof interrupts !== 'undefined') {
// wait for plug-in to be ready
await interrupts['before-action']();
// wait for plug-in manipulations
await interrupts['before-menu-click'](info, tab);
}
else {
console.warn('plugins module is not loaded');
}
//
const {menuItemId, shiftKey} = info;
if (menuItemId === 'whitelist-domain' || menuItemId === 'whitelist-session' || menuItemId === 'whitelist-exact') {
storage(prefs).then(prefs => {
const d = menuItemId !== 'whitelist-session';
const {hostname, protocol = ''} = new URL(tab.url);
let rule;
if (protocol.startsWith('http') || protocol.startsWith('ftp')) {
let whitelist = prefs[d ? 'whitelist' : 'whitelist.session'];
if (menuItemId === 'whitelist-exact') {
rule = 're:^' + tab.url.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$';
}
else {
rule = hostname;
}
whitelist.push(rule);
whitelist = whitelist.filter((h, i, l) => l.indexOf(h) === i);
chrome.storage.local.set({
[d ? 'whitelist' : 'whitelist.session']: whitelist
});
notify(`"${rule}" ${chrome.i18n.getMessage(d ? 'menu_msg1' : 'menu_msg4')}`);
}
else {
notify(`"${protocol}" ${chrome.i18n.getMessage('menu_msg2')}`);
}
});
}
else if (menuItemId === 'discard-tab' || menuItemId === 'discard-tree') {
// it is possible to have multiple highlighted tabs. Let's discard all of them
const tabs = await query({
windowId: tab.windowId
});
const htabs = []; // these are tabs that will be discarded
// discard-tree for Tree Style Tab
if (menuItemId === 'discard-tree' && info.viewType === 'sidebar') {
htabs.push(tab);
await new Promise(resolve => chrome.runtime.sendMessage(TST, {
type: 'get-tree',
tab: tab.id
}, tab => {
const add = tab => {
htabs.push(...tab.children);
tab.children.filter(t => t.children).forEach(add);
};
add(tab);
resolve();
}));
}
// discard-tree for native
else if (tab.highlighted && menuItemId === 'discard-tree') { // if a single not-active tab is called
const tbs = tabs.filter(t => t.highlighted);
if (tbs.length > 1) {
htabs.push(...tbs);
}
else if (tab.groupId && tab.groupId > -1) {
htabs.push(...tabs.filter(t => t.groupId === tab.groupId));
}
else {
htabs.push(tab);
}
}
else {
htabs.push(tab);
}
if (htabs.filter(t => t.active).length) {
// ids to be discarded
const ids = htabs.map(t => t.id);
const otab = tabs
.filter(t => {
return t.discarded === false && t.highlighted === false && t.status !== 'unloaded' &&
ids.indexOf(t.id) === -1 &&
inprogress.has(t.id) === false;
})
.sort((a, b) => Math.abs(a.index - tab.index) - Math.abs(b.index - tab.index))
.shift();
if (otab) {
chrome.tabs.update(otab.id, {
active: true
}, () => {
// at the time we record htabs, one tab was active. Let's mark it as inactive
htabs.forEach(t => t.active = false);
htabs.forEach(discard);
});
}
else {
notify(chrome.i18n.getMessage('menu_msg3'));
}
}
else {
htabs.forEach(discard);
}
}
else if (menuItemId === 'open-tab-then-discard') {
if (isFirefox) {
chrome.tabs.create({
active: false,
url: info.linkUrl,
discarded: true
});
}
else {
chrome.tabs.create({
active: false,
url: info.linkUrl
}, tab => chrome.tabs.executeScript(tab.id, {
runAt: 'document_start',
code: 'window.stop()'
}, () => chrome.tabs.executeScript(tab.id, {
runAt: 'document_start',
file: 'data/lazy.js'
})));
}
}
else if (menuItemId === 'auto-discardable') {
const autoDiscardable = info.value || false; // when called from page context menu, there is no value
try {
chrome.tabs.update(tab.id, {
autoDiscardable
});
}
catch (e) { // Firefox
chrome.tabs.executeScript(tab.id, {
code: `allowed = ${autoDiscardable};`
});
}
}
else if (menuItemId === 'toggle-allowed') {
chrome.tabs.update({
autoDiscardable: tab.autoDiscardable === false
});
}
// discard-tabs, discard-window, discard-other-windows, discard-rights, discard-lefts
// release-tabs, release-window, release-other-windows, release-rights, release-lefts
else {
const info = {
url: '*://*/*',
discarded: menuItemId.startsWith('release'),
active: false
};
if (
['discard-window', 'discard-rights', 'discard-lefts', 'release-window', 'release-rights', 'release-lefts']
.some(k => k === menuItemId)
) {
info.currentWindow = true;
}
else if (menuItemId === 'discard-other-windows' || menuItemId === 'release-other-windows') {
info.currentWindow = false;
}
let tabs = await query(info);
if (menuItemId.endsWith('rights') || menuItemId.endsWith('lefts')) {
if (menuItemId.endsWith('lefts')) {
tabs = tabs.filter(t => t.index < tab.index);
}
else {
tabs = tabs.filter(t => t.index > tab.index);
}
}
if (menuItemId.startsWith('discard')) {
if (shiftKey) {
tabs.forEach(discard);
}
else {
// make sure to only discard possible tabs not all of them
number.check(tabs, number.IGNORE);
}
}
// release
else {
for (const tab of tabs) {
chrome.tabs.reload(tab.id, {
bypassCache: shiftKey ? true : false
});
}
}
}
};
chrome.contextMenus.onClicked.addListener(onClicked);
chrome.browserAction.onClicked.addListener(tab => onClicked({
menuItemId: localStorage.getItem('click')
}, tab));
// commands
chrome.commands.onCommand.addListener(async command => {
if (command.startsWith('move-') || command === 'close') {
navigate(command);
}
else {
const tabs = await query({
active: true,
currentWindow: true
});
if (tabs.length) {
onClicked({
menuItemId: command
}, tabs[0]);
}
}
});
chrome.runtime.onMessage.addListener((request, sender) => {
if (request.method === 'popup') {
query({
active: true,
currentWindow: true
}).then(tabs => {
if (tabs.length) {
onClicked({
menuItemId: request.cmd,
value: request.value,
shiftKey: request.shiftKey
}, tabs[0]);
}
});
}
else if (request.method === 'simulate') {
onClicked({
menuItemId: request.cmd
}, sender.tab);
}
else if (request.method === 'build-context') {
onStartup();
}
});
}