forked from rNeomy/auto-tab-discard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
279 lines (270 loc) · 8.15 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
/* globals discard, query, notify, navigate, starters, prefs */
'use strict';
// Context Menu
{
const TST = 'treestyletab@piro.sakura.ne.jp';
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));
});
arr.splice(1, 0, {
id: 'discard-tree',
title: chrome.i18n.getMessage('menu_discard_tree'),
contexts,
documentUrlPatterns: ['*://*/*']
});
// treestyletab support
const add = () => chrome.runtime.sendMessage(TST, {
type: 'register-self',
name: chrome.runtime.getManifest().name
}, r => {
chrome.runtime.lastError;
if (r === true) {
arr.forEach(params => chrome.runtime.sendMessage(TST, {
type: 'fake-contextMenu-remove',
params
}));
chrome.runtime.sendMessage(TST, {
type: 'fake-contextMenu-remove-all'
}, () => {
arr.forEach(params => chrome.runtime.sendMessage(TST, {
type: 'fake-contextMenu-create',
params
}));
});
}
});
try {
add();
}
catch (e) {}
chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
if (sender.id === TST && request.type === 'ready') {
add();
sendResponse(true);
}
});
};
create([{
id: 'discard-tab',
title: chrome.i18n.getMessage('menu_discard_tab'),
contexts,
documentUrlPatterns: ['*://*/*']
},
{
id: 'discard-window',
title: chrome.i18n.getMessage('menu_discard_window'),
contexts
},
{
id: 'discard-other-windows',
title: chrome.i18n.getMessage('menu_discard_other_windows'),
contexts
},
{
id: 'discard-tabs',
title: chrome.i18n.getMessage('menu_discard_tabs'),
contexts
},
{
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'],
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));
}
const {menuItemId} = info;
if (menuItemId === 'whitelist-domain') {
const {hostname, protocol = ''} = new URL(tab.url);
if (protocol.startsWith('http') || protocol.startsWith('ftp')) {
let {whitelist} = prefs;
whitelist.push(hostname);
whitelist = whitelist.filter((h, i, l) => l.indexOf(h) === i);
chrome.storage.local.set({
whitelist
});
notify(`"${hostname}" ${chrome.i18n.getMessage('menu_msg1')}`);
}
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
htabs.push(...tabs.filter(t => t.highlighted));
}
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 => t.discarded === false && t.highlighted === false && ids.indexOf(t.id) === -1)
.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') {
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 { // discard-tabs, discard-window, discard-other-windows
const info = {
url: '*://*/*',
discarded: false,
active: false
};
if (menuItemId === 'discard-window') {
info.currentWindow = true;
}
else if (menuItemId === 'discard-other-windows') {
info.currentWindow = false;
}
const tabs = await query(info);
const post = tab => new Promise(resolve => chrome.tabs.sendMessage(tab.id, {
method: 'introduce'
}, a => {
chrome.runtime.lastError;
resolve(a);
}));
for (const tab of tabs) {
const a = await post(tab);
if (a && a.exception !== true && a.allowed) {
discard(tab);
}
}
}
};
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(async (request, sender) => {
if (request.method === 'popup') {
const tabs = await query({
active: true,
currentWindow: true
});
if (tabs.length) {
onClicked({
menuItemId: request.cmd,
value: request.value
}, tabs[0]);
}
}
else if (request.method === 'simulate') {
onClicked({
menuItemId: request.cmd
}, sender.tab);
}
else if (request.method === 'build-context') {
onStartup();
}
});
chrome.runtime.onMessageExternal.addListener((request, sender) => {
if (sender.id === TST && request.type === 'fake-contextMenu-click') {
onClicked(request.info, request.tab);
}
});
}