-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
68 lines (61 loc) · 2.07 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
let inNewGroup = {};
chrome.tabs.onCreated.addListener(tab => {
if (tab.pendingUrl != "chrome://newtab/" && tab.openerTabId) {
chrome.tabs.get(tab.openerTabId, openerTab => {
const groupId = openerTab.groupId
if (groupId < 0 ) {
chrome.tabs.group({
tabIds: [
tab.openerTabId,
tab.id
]
});
} else {
const newGroupId = !inNewGroup[tab.openerTabId]
? groupId
: inNewGroup[tab.openerTabId]>1
? inNewGroup[tab.openerTabId]
: undefined;
chrome.tabs.group({
groupId: newGroupId,
tabIds: tab.id
}, id => {
if (inNewGroup[tab.openerTabId]===true) {
inNewGroup[tab.openerTabId] = id
}
});
}
});
}
});
function closeAll(tabs, callback=_=>null) {
for (const tab of tabs) {
console.log({tabs, tab})
chrome.tabs.remove(tab.id, callback)
}
}
chrome.commands.onCommand.addListener(cmd => {
switch (cmd) {
case "toggle-open-in-new-group":
chrome.tabs.query({currentWindow: true, active: true}, e => {
inNewGroup[e[0].id] = !inNewGroup[e[0].id];
});
break;
case "close-current-tab-group":
chrome.tabs.query({currentWindow: true, active: true}, e => {
const groupId = e[0].groupId;
(groupId > 0)
? chrome.tabs.query({groupId}, closeAll)
: closeAll(e)
});
break;
case "create-new-group-from-tabs":
chrome.tabs.query({currentWindow: true, highlighted: true}, e => {
chrome.tabs.group({tabIds: e.map(tab=>tab.id)});
});
break;
default:
console.warn("No cmd handler for:", cmd);
break;
}
});