-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
325 lines (269 loc) · 11.8 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
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
// State ───────────────────────────────────────────────────────────────────────
const state = {}
state.focusedWindowIds = []
chrome.windows.onFocusChanged.addListener((windowId) => {
// Skip WINDOW_ID_NONE
if (windowId === chrome.windows.WINDOW_ID_NONE) {
return
}
// Skip duplicates
const lastFocusedWindowId = state.focusedWindowIds[state.focusedWindowIds.length - 1]
if (windowId === lastFocusedWindowId) {
return
}
// Add the last focused window
state.focusedWindowIds.push(windowId)
// Keep the last two focused windows
state.focusedWindowIds = state.focusedWindowIds.slice(-2)
})
// External ────────────────────────────────────────────────────────────────────
// Cross-extension messaging
// https://developer.chrome.com/extensions/messaging#external
//
// Entry point: Listen for incoming requests.
// Each request has the following format:
// {
// command: String,
// arguments: Array
// }
chrome.runtime.onConnectExternal.addListener((port) => {
port.onMessage.addListener((request) => {
const command = commands[request.command]
const arguments = request.arguments || []
const self = {
port
}
if (command) {
command.apply(self, arguments)
}
})
})
// Commands ────────────────────────────────────────────────────────────────────
const commands = {}
// Get platform ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['get-platform'] = function() {
chrome.runtime.getPlatformInfo((platformInfo) => {
this.port.postMessage({
id: 'get-platform',
platform: platformInfo
})
})
}
// Zoom ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['zoom-in'] = (step = 0.1) => {
chrome.tabs.getZoom(undefined, (zoomFactor) => {
chrome.tabs.setZoom(undefined, zoomFactor + step)
})
}
commands['zoom-out'] = (step = 0.1) => {
commands['zoom-in'](-step)
}
commands['zoom-reset'] = () => {
chrome.tabs.setZoom(undefined, 0)
}
// Open URLs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['open'] = (url) => {
chrome.tabs.update(undefined, { url })
}
commands['download'] = (url) => {
chrome.downloads.download({ url })
}
// Create tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['new-tab'] = (url) => {
const active = url ? false : true
chrome.tabs.create({ url, active })
}
// New tab to the right
commands['new-tab-right'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([openerTab]) => {
chrome.tabs.create({
index: openerTab.index + 1
})
})
}
commands['restore-tab'] = () => {
chrome.sessions.restore()
}
commands['duplicate-tab'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.duplicate(tab.id)
})
}
// Create windows ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['new-window'] = (url) => {
chrome.windows.create({ url })
}
commands['new-incognito-window'] = () => {
chrome.windows.create({ incognito: true })
}
// Close tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['close-tab'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.remove(tab.id)
})
}
commands['close-other-tabs'] = () => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const otherTabIds = tabs.flatMap((tab) => tab.active ? [] : [tab.id])
chrome.tabs.remove(otherTabIds)
})
}
commands['close-right-tabs'] = () => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const activeTab = tabs.find((tab) => tab.active)
const rightTabs = tabs.slice(activeTab.index + 1)
const rightTabIds = rightTabs.map((tab) => tab.id)
chrome.tabs.remove(rightTabIds)
})
}
// Refresh tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['reload-tab'] = (bypassCache = false) => {
chrome.tabs.reload(undefined, { bypassCache })
}
commands['reload-all-tabs'] = (bypassCache = false) => {
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {
chrome.tabs.reload(tab.id, { bypassCache })
}
})
}
// Switch tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['next-tab'] = (count = 1) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const activeTab = tabs.find((tab) => tab.active)
const nextTabIndex = modulo(activeTab.index + count, tabs.length)
const nextTab = tabs[nextTabIndex]
chrome.tabs.update(nextTab.id, { active: true })
})
}
commands['previous-tab'] = (count = 1) => {
commands['next-tab'](-count)
}
commands['first-tab'] = () => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const firstTab = tabs[0]
chrome.tabs.update(firstTab.id, { active: true })
})
}
commands['last-tab'] = () => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const lastTab = tabs[tabs.length - 1]
chrome.tabs.update(lastTab.id, { active: true })
})
}
// Move tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['move-tab-right'] = (count = 1) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const activeTab = tabs.find((tab) => tab.active)
const nextTabIndex = modulo(activeTab.index + count, tabs.length)
const nextTab = tabs[nextTabIndex]
chrome.tabs.move(activeTab.id, { index: nextTab.index })
})
}
commands['move-tab-left'] = (count = 1) => {
commands['move-tab-right'](-count)
}
commands['move-tab-first'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.move(tab.id, { index: 0 })
})
}
commands['move-tab-last'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.move(tab.id, { index: -1 })
})
}
// Detach tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['detach-tab'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
const pinned = tab.pinned
chrome.windows.create({ tabId: tab.id }, (window) => {
chrome.tabs.update(tab.id, { pinned })
})
})
}
// Moves the current tab to the right of the active tab of the previous window
// and activates it.
commands['attach-tab'] = () => {
const lastFocusedWindowId = state.focusedWindowIds[state.focusedWindowIds.length - 2]
chrome.tabs.query({ windowId: lastFocusedWindowId }, (tabs) => {
const targetTab = tabs.find((tab) => tab.active)
const pinned = targetTab.pinned
const targetedTabIndex = modulo(targetTab.index + 1, tabs.length + 1)
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
// Issue: Moving a tab to another window does not preserve the pinned state.
chrome.tabs.move(tab.id, { windowId: targetTab.windowId, index: -1 }, (tab) => {
// Make the tab active in its window.
chrome.tabs.update(tab.id, { active: true, pinned })
chrome.tabs.move(tab.id, { index: targetedTabIndex })
})
})
})
}
// Moves the tabs in the current window to the right of the active tab of the previous window.
commands['attach-tabs'] = () => {
const lastFocusedWindowId = state.focusedWindowIds[state.focusedWindowIds.length - 2]
chrome.tabs.query({ windowId: lastFocusedWindowId }, (tabs) => {
const targetTab = tabs.find((tab) => tab.active)
const pinned = targetTab.pinned
const targetedTabIndex = modulo(targetTab.index + 1, tabs.length + 1)
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const tabIds = tabs.map((tab) => tab.id)
// Issue: Moving a tab to another window does not preserve the pinned state.
chrome.tabs.move(tabIds, { windowId: targetTab.windowId, index: -1 }, (tabs) => {
for (const tabId of tabIds) {
chrome.tabs.update(tabId, { pinned })
}
chrome.tabs.move(tabIds, { index: targetedTabIndex })
})
})
})
}
// Discard tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['discard-tab'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.discard(tab.id)
})
}
// Mute tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['mute-tab'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.update(tab.id, { muted: ! tab.mutedInfo.muted })
})
}
let muted = false
commands['mute-all-tabs'] = () => {
muted = ! muted
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {
chrome.tabs.update(tab.id, { muted })
}
})
}
// Pin tabs ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['pin-tab'] = () => {
chrome.tabs.query({ currentWindow: true, active: true }, ([tab]) => {
chrome.tabs.update(tab.id, { pinned: ! tab.pinned })
})
}
// Notifications ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
commands['notify'] = (id, options) => {
const properties = {}
properties.title = ''
properties.message = ''
properties.type = 'basic'
properties.iconUrl = 'packages/tools.svg'
Object.assign(properties, options)
chrome.notifications.getAll((notifications) => {
const notification = notifications[id]
if (notification) {
chrome.notifications.update(id, properties)
} else {
chrome.notifications.create(id, properties)
}
})
}
// Helpers ─────────────────────────────────────────────────────────────────────
const modulo = (dividend, divisor) => {
return ((dividend % divisor) + divisor) % divisor
}