-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
45 lines (43 loc) · 1.32 KB
/
background.js
File metadata and controls
45 lines (43 loc) · 1.32 KB
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
function processCommand(command, tab, sameTabs) {
const minIndex = Math.min(...sameTabs.map((t) => t.index));
const maxIndex = Math.max(...sameTabs.map((t) => t.index));
switch (command) {
case "move-tab-first":
browser.tabs.move(tab.id, { index: minIndex });
break;
case "move-tab-left":
if (tab.index > minIndex) {
browser.tabs.move(tab.id, { index: tab.index - 1 });
}
break;
case "move-tab-right":
if (tab.index < maxIndex) {
browser.tabs.move(tab.id, { index: tab.index + 1 });
}
break;
case "move-tab-last":
browser.tabs.move(tab.id, { index: maxIndex });
break;
case "duplicate-tab":
browser.tabs.duplicate(tab.id);
break;
case "close-tab":
browser.tabs.remove(tab.id);
break;
case "pin-tab":
browser.tabs.update(tab.id, { pinned: !tab.pinned });
break;
default:
console.log(`command "${command}" not supported`);
}
}
browser.commands.onCommand.addListener((command) => {
Promise.all([
browser.tabs.query({ currentWindow: true, active: true }),
browser.tabs.query({ currentWindow: true }),
]).then(([tabs, allTabs]) => {
const tab = tabs[0];
const sameTabs = allTabs.filter((t) => t.pinned === tab.pinned);
processCommand(command, tab, sameTabs);
});
});