-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
76 lines (74 loc) · 2.25 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
function logHelper(command, tab, pinnedTabCount) {
console.log(`command: ${command}
tab-title: ${tab.title}
tab-id: ${tab.id}
pinnned: ${tab.pinned}
pinned-count: ${pinnedTabCount}`);
}
function processCommand(command, tab, pinnedTabCount) {
//logHelper(command, tab, pinnedTabCount);
switch (command) {
case "move-tab-first":
if (pinnedTabCount > 0 && !tab.pinned) {
browser.tabs.move(tab.id, { index: pinnedTabCount });
} else {
browser.tabs.move(tab.id, { index: 0 });
}
break;
case "move-tab-left":
if (tab.index > 0) {
browser.tabs.move(tab.id, { index: tab.index - 1 });
}
break;
case "move-tab-right":
browser.tabs.move(tab.id, { index: tab.index + 1 });
break;
case "move-tab-last":
if (pinnedTabCount > 0 && tab.pinned) {
browser.tabs.move(tab.id, { index: pinnedTabCount - 1 });
} else {
// hacky to move to last placement hopefully we never have 100+ tabs :facepalm:
browser.tabs.move(tab.id, { index: 100 });
}
break;
case "pin-tab":
if (tab.pinned) {
browser.tabs.update(tab.id, { pinned: false });
} else {
browser.tabs.update(tab.id, { pinned: true });
}
break;
case "duplicate-tab":
browser.tabs.duplicate(tab.id);
break;
case "close-tab":
browser.tabs.remove(tab.id);
break;
default:
console.log(`command "${command}" not supported`);
}
}
browser.commands.onCommand.addListener((command) => {
// run tab queries in parrallel
Promise.allSettled([
// query for active tab in my current window
browser.tabs.query({ currentWindow: true, active: true }),
// query for pinned tabs to help with placement
browser.tabs.query({ pinned: true }),
]).then((tabQueryResults) => {
let activeTabResult = tabQueryResults[0];
if (activeTabResult.status != "fulfilled") {
console.log(activeTabResult.reason);
}
let pinnedTabsResult = tabQueryResults[1];
if (pinnedTabsResult.status != "fulfilled") {
console.log(pinnedTabsResult.reason);
}
processCommand(
command,
// resulting activeTab should be an array of 1
activeTabResult.value[0],
pinnedTabsResult.value.length
);
});
});