-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
36 lines (28 loc) · 980 Bytes
/
popup.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
const groupTabsButton = document.querySelector("#groupTabsButton");
groupTabsButton.addEventListener("click", async () => {
const tabs = await chrome.tabs.query({ currentWindow: true });
const ungroupedTabInfo = tabs.map((tab) => {
const baseUrlMatch = tab.url.match(/https:\/\/([\w\d.]*)\//);
if (baseUrlMatch === null) {
return { id: tab.id, baseUrl: null };
}
return { id: tab.id, baseUrl: baseUrlMatch[1] };
});
const groupedTabIds = ungroupedTabInfo.reduce(
(acc, cur) => {
if (cur.baseUrl === null) {
return { ...acc, 0: [...acc["0"], cur.id] };
}
if (Object.keys(acc).includes(cur.baseUrl)) {
return { ...acc, [cur.baseUrl]: [...acc[cur.baseUrl], cur.id] };
}
return { ...acc, [cur.baseUrl]: [cur.id] };
},
{ 0: [] }
);
Object.values(groupedTabIds).forEach((tabIdList) => {
tabIdList.forEach((tabId) => {
chrome.tabs.move(tabId, { index: -1 });
});
});
});