This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
tab.js
228 lines (197 loc) · 7.61 KB
/
tab.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
import { posMod } from 'lib/util'
import browser from 'webextension-polyfill'
/** Activate the tab to the right of the active tab of the current window */
export async function nextTab () {
const tabs = await browser.tabs.query({ currentWindow: true })
const activeTabIndex = tabs.findIndex(tab => tab.active)
const nextTabId = tabs[posMod(activeTabIndex + 1, tabs.length)].id
await browser.tabs.update(nextTabId, { active: true })
}
/** Activate the tab to the left of the active tab of the current window */
export async function previousTab () {
const tabs = await browser.tabs.query({ currentWindow: true })
const activeTabIndex = tabs.findIndex(tab => tab.active)
const previousTabId = tabs[posMod(activeTabIndex - 1, tabs.length)].id
await browser.tabs.update(previousTabId, { active: true })
}
/** Activate the first tab of the current window */
export async function firstTab () {
const tabs = await browser.tabs.query({ currentWindow: true })
const firstTabId = tabs[0].id
await browser.tabs.update(firstTabId, { active: true })
}
/** Activate the last tab of the current window */
export async function lastTab () {
const tabs = await browser.tabs.query({ currentWindow: true })
const lastTabId = tabs[tabs.length - 1].id
await browser.tabs.update(lastTabId, { active: true })
}
/** Move the active tab of the current window left */
export async function moveTabLeft () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.move(tab.id, { index: tab.index - 1 })
}
/** Move the active tab of the current window right */
export async function moveTabRight () {
const tabs = await browser.tabs.query({ currentWindow: true })
const activeTabIndex = tabs.findIndex(tab => tab.active)
const nextTabIndex = posMod(activeTabIndex + 1, tabs.length)
await browser.tabs.move(tabs[activeTabIndex].id, { index: nextTabIndex })
}
/** Move the active tab of the current window to the far left */
export async function moveTabFirst () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.move(tab.id, { index: 0 })
}
/** Move the active tab of the current window to the far right */
export async function moveTabLast () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.move(tab.id, { index: -1 })
}
/** Move the active tab of the current window to a new window */
export async function moveTabNewWindow () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.windows.create({ tabId: tab.id })
}
/** Close the active tab of the current window */
export async function closeTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.remove(tab.id)
}
/** Close all tabs of the current window except the active tab */
export async function closeOtherTabs () {
const tabs = await browser.tabs.query({ currentWindow: true })
await Promise.all(
tabs.map(async tab => {
if (!tab.active) await browser.tabs.remove(tab.id)
})
)
}
/** Close all tabs to the right of the active tab of the current window */
export async function closeRightTabs () {
const tabs = await browser.tabs.query({ currentWindow: true })
const activeTabIndex = tabs.findIndex(tab => tab.active)
await Promise.all(
tabs.slice(activeTabIndex + 1).map(async tab => {
await browser.tabs.remove(tab.id)
})
)
}
/** Close all tabs to the left of the active tab of the current window */
export async function closeLeftTabs () {
const tabs = await browser.tabs.query({ currentWindow: true })
const activeTabIndex = tabs.findIndex(tab => tab.active)
await Promise.all(
tabs.slice(0, activeTabIndex).map(async tab => {
await browser.tabs.remove(tab.id)
})
)
}
/** Create and activate a new tab */
export async function newTab () {
await browser.tabs.create({})
}
/** Reopen and activate the last closed tab */
export async function restoreTab () {
await browser.sessions.restore()
}
/** Duplicate and activate the active tab of the current window */
export async function duplicateTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.duplicate(tab.id)
}
/** Duplicate and activate the active tab of the current window incognito */
export async function duplicateTabIncognito () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
if (tab.incognito) {
await browser.tabs.duplicate(tab.id)
} else {
const activeWindows = await browser.windows.getAll({
windowTypes: ['normal']
})
const incognitoWindows = activeWindows.filter(w => w.incognito)
if (incognitoWindows.length > 0) {
await browser.tabs.create({
url: tab.url,
windowId: incognitoWindows[0].id
})
} else {
await browser.windows.create({
url: tab.url,
incognito: true,
state: 'maximized'
})
}
}
}
/** Create a new window */
export async function newWindow () {
await browser.windows.create()
}
/** Create a new incognito window */
export async function newIncognitoWindow () {
await browser.windows.create({ incognito: true })
}
/** Activate the next window */
export async function switchWindow () {
// TODO: Implement
console.error('switchWindow not implemented')
}
const zoomStep = 10 // for 10%
/** Zoom in the active tab of the current window */
export async function zoomIn () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
const zoomFactor = await browser.tabs.getZoom()
await browser.tabs.setZoom(tab.id, Math.min(zoomFactor + zoomStep / 100, 3.0))
}
/** Zoom out the active tab of the current window */
export async function zoomOut () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
const zoomFactor = await browser.tabs.getZoom()
await browser.tabs.setZoom(tab.id, Math.max(zoomFactor - zoomStep / 100, 0.3))
}
/** Reset the zoom of the active tab of the current window */
export async function zoomReset () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.setZoom(tab.id, 0)
}
/** Refresh the active tab of the current window */
export async function refreshTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.reload(tab.id)
}
/** Refresh all tabs of the current window */
export async function refreshAllTabs () {
const tabs = await browser.tabs.query({ currentWindow: true })
await Promise.all(
tabs.map(async tab => {
await browser.tabs.reload(tab.id)
})
)
}
/** Hard refreshes the active tab of the current window */
export async function hardRefreshTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.reload(tab.id, { bypassCache: true })
}
/** Toggle the mute state of the active tab of the current window */
export async function toggleMuteTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.update(tab.id, { muted: !tab.mutedInfo.muted })
}
let muted = false
/** Mute all tabs of all windows */
export async function toggleMuteAllTabs () {
muted = !muted
const tabs = await browser.tabs.query({})
await Promise.all(
tabs.map(async tab => {
await browser.tabs.update(tab.id, { muted })
})
)
}
/** Toggle the pin state of the active tab of the current window */
export async function togglePinTab () {
const [tab] = await browser.tabs.query({ currentWindow: true, active: true })
await browser.tabs.update(tab.id, { pinned: !tab.pinned })
}