-
Notifications
You must be signed in to change notification settings - Fork 0
/
rearrange.js
140 lines (123 loc) · 3.8 KB
/
rearrange.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
130
131
132
133
134
135
136
137
138
139
140
/* global chrome */
chrome.commands.onCommand.addListener(command => {
const indexStep = {
'move-tab-left': -1,
'move-tab-right': 1,
'move-tab-to-next-window': 1,
}[command];
const getNumTabsInCurrentWindow = () => {
return new Promise((resolve, reject) => {
chrome.windows.getCurrent({ populate: true }, window => {
resolve(window.tabs.length);
});
});
};
const getNumPinnedTabsInCurrentWindow = () => {
return new Promise((resolve, reject) => {
chrome.tabs.query(
{
currentWindow: true,
pinned: true,
},
tabs => {
resolve(tabs.length);
}
);
});
};
const shiftTabInWindow = tabs => {
const currentTab = tabs[0];
if (!currentTab) {
return;
}
getNumTabsInCurrentWindow().then(numTabs => {
getNumPinnedTabsInCurrentWindow().then(numPinnedTabs => {
const numUnPinnedTabs = numTabs - numPinnedTabs;
let newIndex = currentTab.index + indexStep;
if (currentTab.pinned) {
newIndex = newIndex % numPinnedTabs;
if (newIndex === -1) {
newIndex = numPinnedTabs - 1;
}
} else {
newIndex = ((newIndex - numPinnedTabs) % numUnPinnedTabs) + numPinnedTabs;
if (newIndex === numPinnedTabs - 1) {
newIndex = -1;
}
}
chrome.tabs.move(currentTab.id, { index: newIndex });
});
});
};
const moveTabToWindow = tabs => {
const currentTab = tabs[0];
chrome.windows.getAll({ windowTypes: ['normal'] }, windows => {
const currentWindowIndex = windows.findIndex(window => window.focused);
let newIndex = (currentWindowIndex + indexStep) % windows.length;
if (newIndex === -1) {
newIndex = windows.length - 1;
}
const newWindow = windows[newIndex];
chrome.tabs.move(currentTab.id, { index: -1, windowId: newWindow.id });
chrome.windows.update(newWindow.id, { focused: true });
chrome.tabs.update(currentTab.id, {
active: true,
pinned: currentTab.pinned,
});
});
};
const moveTabToNewWindow = tabs => {
const currentTab = tabs[0];
chrome.windows.getCurrent({ populate: true }, window => {
if (window.tabs.length !== 1) {
chrome.windows.create({ tabId: currentTab.id });
}
});
};
const moveMultipleTabsToNewWindow = tabs => {
const numTabs = parseInt(prompt('How many tabs?'));
if (isNaN(numTabs)) {
return;
}
const currentTabIndex = tabs.findIndex(tab => tab.active);
const tabsToMove = tabs.slice(Math.max(0, currentTabIndex - numTabs + 1), currentTabIndex);
chrome.windows.getCurrent({ populate: true }, window => {
if (window.tabs.length !== 1) {
chrome.windows.create({ tabId: tabs[currentTabIndex].id }, newWindow => {
tabsToMove.reverse().forEach(tab => {
chrome.tabs.move(tab.id, { index: 0, windowId: newWindow.id });
});
});
}
});
};
const executeCommand = commandCallback => {
[true, false].forEach(pinned => {
chrome.tabs.query(
{
active: true,
currentWindow: true,
pinned,
},
commandCallback
);
});
};
if (['move-tab-left', 'move-tab-right'].includes(command)) {
executeCommand(shiftTabInWindow);
} else if (command === 'move-tab-to-next-window') {
executeCommand(moveTabToWindow);
} else if (command === 'move-tab-to-new-window') {
executeCommand(moveTabToNewWindow);
} else if (command === 'move-multiple-tabs-to-new-window') {
chrome.tabs.query(
{
currentWindow: true,
pinned: false,
},
moveMultipleTabsToNewWindow
);
} else {
alert(`Tab rearranger doesn't recognize the command "${command}"`);
}
});