-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
57 lines (50 loc) · 1.86 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
// A lightweight google chrome tab optimizer by @n0tst3
async function stackTabs() {
const tabs = await chrome.tabs.query({ currentWindow: true });
const tabsByDomain = groupTabsByDomain(tabs);
await createTabGroups(tabsByDomain);
}
function groupTabsByDomain(tabs) {
let tabsByDomain = {};
tabs.forEach((tab) => {
if (isValidURL(tab.url)) {
const url = new URL(tab.url);
const domain = url.hostname;
if (tabsByDomain[domain]) {
tabsByDomain[domain].push(tab.id);
} else {
tabsByDomain[domain] = [tab.id];
}
}
});
return tabsByDomain;
}
async function createTabGroups(tabsByDomain) {
for (const domain in tabsByDomain) {
const tabIds = tabsByDomain[domain];
const groupId = await chrome.tabs.group({ tabIds: tabIds });
const tabsInGroup = tabIds.length;
const tabIndices = tabsInGroup > 1 ? [0] : [0, 1];
await chrome.tabs.highlight({ tabs: tabIndices });
}
}
function autoSortTabsByDomain(enabled) {
if (enabled) {
chrome.tabs.onCreated.addListener(onTabCreate);
chrome.tabs.onUpdated.addListener(onTabUpdate);
stackTabs();
} else {
chrome.tabs.onCreated.removeListener(onTabCreate);
chrome.tabs.onUpdated.removeListener(onTabUpdate);
}
}
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("stack-tabs-btn").addEventListener("click", stackTabs);
chrome.storage.sync.get("autoSortEnabled", function (data) {
document.getElementById("auto-sort-checkbox").checked = data.autoSortEnabled;
});
document.getElementById("auto-sort-checkbox").addEventListener("change", function () {
var enabled = this.checked;
chrome.storage.sync.set({ autoSortEnabled: enabled }, function () { });
});
});