-
Notifications
You must be signed in to change notification settings - Fork 4
/
service_worker.js
85 lines (73 loc) · 2.62 KB
/
service_worker.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
chrome.commands.onCommand.addListener(function(command) {
let numTabsInCurrentWindow;
chrome.tabs.query({ currentWindow: true },
function (tabs) { numTabsInCurrentWindow = tabs.length; }
);
switch (command) {
case "move-tabs-left":
processHighlightedTabs(function (tabs) {
for (let i = 0; i < tabs.length; i++) {
moveOneTabInDirection(tabs[i], -1);
}
});
break;
case "move-tabs-right":
processHighlightedTabs(function (tabs) {
for (let i = tabs.length - 1; i >= 0; i--) {
moveOneTabInDirection(tabs[i], 1);
}
});
break;
case "undock-tabs-to-new-window":
let activeTab;
chrome.tabs.query({ currentWindow: true, active: true },
function (tabs) { activeTab = tabs[0]; }
);
processHighlightedTabs(function (tabs) {
chrome.windows.create({ tabId: tabs[0].id }, function (window) {
tabs.shift();
if (tabs.length > 0) {
chrome.tabs.move(tabs.map(tab => tab.id), { windowId: window.id, index: 1 });
chrome.tabs.update(activeTab.id, { active: true });
}
});
});
break;
case "move-tabs-between-windows":
chrome.windows.getAll({ populate: true },
function (windows) {
if (windows.length < 2) return;
chrome.windows.getCurrent(
function (currentWindow) {
let nextWindowIndex = windows.map(window => window.id).indexOf(currentWindow.id) + 1;
if (nextWindowIndex >= windows.length) nextWindowIndex = 0;
let nextWindow = windows[nextWindowIndex];
processHighlightedTabs(function (tabs) {
chrome.tabs.query({ currentWindow: true, active: true },
function (activeTabs) {
let activeTab = activeTabs[0];
chrome.tabs.move(tabs.map(tab => tab.id), { windowId: nextWindow.id, index: nextWindow.tabs.length });
chrome.tabs.update(activeTab.id, { active: true });
chrome.windows.update(nextWindow.id, { focused: true });
}
);
});
}
);
}
);
break;
}
function processHighlightedTabs(callback) {
chrome.tabs.query({ currentWindow: true, highlighted: true }, callback);
}
function moveOneTabInDirection(tab, direction) {
let index = tab.index + direction;
if (index >= numTabsInCurrentWindow) {
index = numTabsInCurrentWindow - 1;
} else if (index < 0) {
index = 0;
}
chrome.tabs.move(tab.id, { index: index });
}
});