-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
65 lines (52 loc) · 1.5 KB
/
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
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
const IGNORED_URLS = [
'chrome://',
'file://',
];
const groupTabsButton = document.getElementById('groupTabsButton');
groupTabsButton.addEventListener("click", async () => {
const { groups } = await prepareGroups();
groupTabs({ groups }).catch((err) => {
console.log('grouping tabs failed', err);
})
});
async function prepareGroups() {
const tabLookup = {};
const tabs = await chrome.tabs.query({});
tabs.forEach((tab) => {
const url = tab.url;
if (isUrlAvailableToGroup({ url })) {
const { baseUrl } = getBaseUrl({ url });
if (!tabLookup[baseUrl]) {
tabLookup[baseUrl] = {
tabIds: [],
};
}
tabLookup[baseUrl].tabIds.push(tab.id);
}
});
return {
groups: tabLookup,
};
}
async function groupTabs({ groups }) {
Object.keys(groups).forEach(async (key) => {
if (groups[key].tabIds.length > 1) {
const groupId = await chrome.tabs.group({
tabIds: groups[key].tabIds,
});
chrome.tabGroups.update(groupId, {
collapsed: true,
title: key,
});
}
});
}
function isUrlAvailableToGroup({ url }) {
return !IGNORED_URLS.some((ignoredUrl) => url.startsWith(ignoredUrl));
}
function getBaseUrl({ url }) {
const hostname = new URL(url).hostname.replace('www.', '');
return {
baseUrl: hostname.split('.')[0],
};
}