forked from mohamedmansour/reload-all-tabs-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreload_controller.js
350 lines (307 loc) · 8.1 KB
/
reload_controller.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
async function getSetting(keys) {
return new Promise((resolve) => {
chrome.storage.sync.get(keys, (values) => {
let results = {}
keys.forEach(key => {
let result = undefined
const value = values[key]
switch (key) {
case 'version':
result = value
break
case 'buttonDefaultAction':
result = (typeof value == 'undefined') ? 'window' : value
break
case 'reloadWindow':
result = (value == 'undefined') ? true : (value == true)
break
case 'reloadAllWindows':
case 'reloadPinnedOnly':
case 'reloadUnpinnedOnly':
case 'reloadAllRight':
case 'reloadAllLeft':
case 'closeAllRight':
case 'closeAllLeft':
case 'bypassCache':
result = value == true
break
default:
result = undefined
break
}
results[key] = result
})
resolve(results)
})
})
}
/**
* Initializes the reload extension.
*/
async function init() {
chrome.action.onClicked.addListener(async () => await reload())
chrome.storage.onChanged.addListener(async (changes) => await onStorageChanged(changes))
chrome.commands.onCommand.addListener(async () => await reload());
await updateContextMenu()
// Version Check.
const currVersion = chrome.runtime.getManifest().version
const { version } = await getSetting(['version'])
if (currVersion != version) {
// Check if we just installed this extension.
if (typeof version == 'undefined') {
onInstall()
}
// Update the version incase we want to do something in future.
chrome.storage.sync.set({ 'version': currVersion })
}
}
async function onStorageChanged(changes) {
for (key in changes) {
if (key.startsWith('reload') || key == 'bypassCache' || key.startsWith('close')) {
await updateContextMenu()
}
}
}
function onMenuClicked(info) {
switch (info.menuItemId) {
case 'reloadWindow':
chrome.windows.getCurrent((win) => reloadWindow(win))
break
case 'reloadAllWindows':
reloadAllWindows()
break
case 'reloadPinnedOnly':
chrome.windows.getCurrent((win) => reloadWindow(win, { reloadPinnedOnly: true }))
break
case 'reloadUnpinnedOnly':
chrome.windows.getCurrent((win) => reloadWindow(win, { reloadUnpinnedOnly: true }))
break
case 'reloadAllLeft':
chrome.windows.getCurrent((win) => reloadWindow(win, { reloadAllLeft: true }))
break
case 'reloadAllRight':
chrome.windows.getCurrent((win) => reloadWindow(win, { reloadAllRight: true }))
break
case 'closeAllLeft':
chrome.windows.getCurrent((win) => closeWindow(win, { closeAllLeft: true }))
break
case 'closeAllRight':
chrome.windows.getCurrent((win) => closeWindow(win, { closeAllRight: true }))
break
default:
break
}
}
/**
* Reload Routine. It checks which option the user has allowed (All windows, or
* or just the current window) then initiates the request.
*/
async function reload() {
const { buttonDefaultAction } = await getSetting(['buttonDefaultAction'])
switch (buttonDefaultAction) {
case 'allWindows':
reloadAllWindows()
break
case 'pinned':
chrome.windows.getCurrent((win) => reloadWindow(win, { reloadPinnedOnly: true }))
break
case 'unpinned':
chrome.windows.getCurrent((win) => reloadWindow(win, { reloadUnpinnedOnly: true }))
break
default:
chrome.windows.getCurrent((win) => this.reloadWindow(win))
break
}
}
/**
* Handles the request coming back from an external extension.
*/
async function updateContextMenu() {
chrome.contextMenus.removeAll()
chrome.contextMenus.onClicked.addListener((info) => onMenuClicked(info))
const setting = await getSetting([
'bypassCache',
'reloadWindow',
'reloadAllWindows',
'reloadPinnedOnly',
'reloadUnpinnedOnly',
'reloadAllLeft',
'reloadAllRight',
'closeAllLeft',
'closeAllRight'
])
let attributions = ''
if (setting.bypassCache) {
attributions = ' (cache bypassed)'
}
if (setting.reloadWindow) {
chrome.contextMenus.create({
id: 'reloadWindow',
type: 'normal',
title: `Reload this window${attributions}`,
contexts: ['all']
})
}
if (setting.reloadAllWindows) {
chrome.contextMenus.create({
id: 'reloadAllWindows',
type: 'normal',
title: `Reload all windows${attributions}`,
contexts: ['all']
})
}
if (setting.reloadPinnedOnly) {
chrome.contextMenus.create({
id: 'reloadPinnedOnly',
type: 'normal',
title: `Reload pinned tabs${attributions}`,
contexts: ['all']
})
}
if (setting.reloadUnpinnedOnly) {
chrome.contextMenus.create({
id: 'reloadUnpinnedOnly',
type: 'normal',
title: `Reload unpinned tabs${attributions}`,
contexts: ['all']
})
}
if (setting.reloadAllLeft) {
chrome.contextMenus.create({
id: 'reloadAllLeft',
type: 'normal',
title: `Reload all tabs to the left${attributions}`,
contexts: ['all']
})
}
if (setting.reloadAllRight) {
chrome.contextMenus.create({
id: 'reloadAllRight',
type: 'normal',
title: `Reload all tabs to the right${attributions}`,
contexts: ['all']
})
}
if (setting.closeAllLeft) {
chrome.contextMenus.create({
id: 'closeAllLeft',
type: 'normal',
title: `Close all tabs to the left${attributions}`,
contexts: ['all']
})
}
if (setting.closeAllRight) {
chrome.contextMenus.create({
id: 'closeAllRight',
type: 'normal',
title: `Close all tabs to the right${attributions}`,
contexts: ['all']
})
}
}
/**
* When the extension first installed.
*/
function onInstall() {
chrome.runtime.openOptionsPage()
}
/**
* Close tabs to left or right one by one.
*
* @param win Window to close.
*/
function closeWindow(win, options = {}) {
chrome.tabs.query({ windowId: win.id }, (tabs) => {
const tabsToClose = []
let passedCurrent = false
for (const i in tabs) {
const tab = tabs[i]
if (tab.active) {
passedCurrent = true
continue
}
if (passedCurrent) { // right of current
if (options.closeAllLeft) {
break
}
if (options.closeAllRight) {
tabsToClose.push(tab.id)
}
} else if (options.closeAllLeft) {
tabsToClose.push(tab.id)
}
}
if (tabsToClose.length) {
chrome.tabs.remove(tabsToClose).then(() => { })
}
})
}
/**
* Reload all |tabs| one by one.
*
* @param win Window to reload.
*/
function reloadWindow(win, options = {}) {
chrome.tabs.query({ windowId: win.id }, async (tabs) => {
const strategy = {}
for (const i in tabs) {
const tab = tabs[i]
await reloadStrategy(tab, strategy, options)
}
})
}
// When this gets complicated, create a strategy pattern.
async function reloadStrategy(tab, strategy, options = {}) {
let issueReload = true
if (options.reloadPinnedOnly && !tab.pinned) {
issueReload = false
}
if (options.reloadUnpinnedOnly && tab.pinned) {
issueReload = false
}
if (options.reloadAllLeft) {
if (tab.active) {
strategy.stop = true
}
if (strategy.stop) {
issueReload = false
}
}
if (options.reloadAllRight) {
if (!strategy.reset) {
if (!tab.active) {
strategy.stop = true
}
else {
strategy.reset = true
}
}
if (strategy.stop) {
issueReload = false
if (strategy.reset) {
strategy.stop = false
}
}
}
if (issueReload) {
const { bypassCache } = await getSetting(['bypassCache'])
console.log(`reloading ${tab.url}, cache bypassed: ${bypassCache}`)
chrome.tabs.reload(tab.id, { bypassCache }, null)
}
}
/**
* Reload all tabs in all windows one by one.
*/
function reloadAllWindows() {
chrome.windows.getAll({}, (windows) => {
for (const i in windows) {
reloadWindow(windows[i])
}
})
}
try {
init()
}
catch (e) {
console.error(e)
}