-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
129 lines (108 loc) · 3.24 KB
/
sw.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
// Default options
async function setDefaults() {
// Get options
const renamedOptions = {
switchWindows: 'dual',
wrapFirstLast: 'skip',
};
const defaultOptions = {
skipCollapsed: false,
switchWindows: false,
wrapFirstLast: false,
};
const currentOptions = await chrome.storage.sync.get();
// Get keys of bad options
const removingOptions = [];
for (const key of Object.keys(currentOptions)) {
if (!(key in defaultOptions)) {
removingOptions.push(key);
}
}
// Remove bad options
if (Object.keys(removingOptions).length) {
chrome.storage.sync.remove(removingOptions);
}
// Get keys & values of unsaved options
const unsavedOptions = {};
for (const [key, value] of Object.entries(defaultOptions)) {
if (currentOptions[key] === undefined) {
// Get value from old key
const oldKey = renamedOptions[key];
if (oldKey in currentOptions) {
unsavedOptions[key] = currentOptions[oldKey];
}
// Get value from default
else {
unsavedOptions[key] = value;
}
}
}
// Save unsaved options
if (Object.keys(unsavedOptions).length) {
chrome.storage.sync.set(unsavedOptions);
}
}
setDefaults();
// Tab switching
const collapsedGroupIds = new Set();
async function switchTab(command) {
// Get options, windows with tabs, & collapsed groups
const [options, windows, collapsedGroups] = await Promise.all([
chrome.storage.sync.get(),
chrome.windows.getAll({ populate: true }),
chrome.tabGroups.query({ collapsed: true }),
]);
// Sort windows according to horizontal position
if (windows.length)
windows.sort((a, b) => a.left - b.left);
// Get all collapsed group ids
for (let i = 0; i < collapsedGroups.length; i++)
collapsedGroupIds.add(collapsedGroups[i].id);
// Make a continuous array of only some tabs
const tabs = [];
let currentIndex = null;
// Each window
for (let i = 0; i < windows.length; i++) {
const window = windows[i];
// Filter out minimized windows
if (window.state === 'minimized')
continue;
// Filter out other windows if window-switching option is disabled
if (!options.switchWindows && !window.focused)
continue;
// Each tab
for (let j = 0; j < window.tabs.length; j++) {
const tab = window.tabs[j];
// Filter out tabs in collapsed groups
if (options.skipCollapsed && collapsedGroupIds.has(tab.groupId))
continue;
// Remember where the current tab is in the array
if (window.focused && tab.active)
currentIndex = tabs.length;
// Push important tab info to array
tabs.push({
windowId: window.id,
tabId: tab.id,
});
}
}
// Skip if there is no current tab, like in DevTools
if (currentIndex === null)
return;
// Change the index
let newIndex = currentIndex + ((command === 'leftTab') ? -1 : 1);
if (newIndex < 0)
newIndex = (options.wrapFirstLast) ? (tabs.length - 1) : 0;
else if (newIndex >= tabs.length)
newIndex = (options.wrapFirstLast) ? 0 : (tabs.length - 1);
// Get id for new window & new tab
const windowId = tabs[newIndex].windowId;
const tabId = tabs[newIndex].tabId;
// Switch window & tab
if (windowId)
await chrome.windows.update(windowId, {focused: true});
chrome.tabs.update(tabId, {active: true});
// Clear all collapsed group ids
collapsedGroupIds.clear();
}
chrome.commands.onCommand.addListener(switchTab);