diff options
| author | 2026-02-01 22:38:39 +0300 | |
|---|---|---|
| committer | 2026-02-01 22:38:39 +0300 | |
| commit | 50357f4764140855e8657468e11832debfb37e6d (patch) | |
| tree | c12b5e3dc73a4b6cf53ce6c17dc27db368eca2a1 /group.js | |
| download | grouper-50357f4764140855e8657468e11832debfb37e6d.tar.gz grouper-50357f4764140855e8657468e11832debfb37e6d.tar.bz2 grouper-50357f4764140855e8657468e11832debfb37e6d.tar.xz grouper-50357f4764140855e8657468e11832debfb37e6d.zip | |
Diffstat (limited to 'group.js')
| -rw-r--r-- | group.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/group.js b/group.js new file mode 100644 index 0000000..a399846 --- /dev/null +++ b/group.js @@ -0,0 +1,89 @@ +browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { + if (changeInfo.url) { + groupTabs(tabId); + } +}); + +const colors = [ + "blue", + "cyan", + "grey", + "green", + "orange", + "pink", + "purple", + "red", + "yellow", +]; + +async function groupTabs(tabId) { + const tabs = await browser.tabs.query({ currentWindow: true }); + + const domainGroups = {}; + const groupIds = []; + + for (let tab of tabs) { + if (!groupIds.includes(tab.groupId)) { + groupIds.push(tab.groupId); + } + + const hostname = new URL(tab.url).hostname; + + const parts = hostname.split("."); + + let domain = parts[0]; + if (parts.length > 1) { + parts.pop(); + if (parts[0] == "www") { + parts.shift(); + } + domain = toTitleCase(parts.join(" ")); + } + + if (domain == "") { + domain = "Browser"; + } + + if (tab.groupId == -1 || tab.id == tabId || tab.active) { + if (!domainGroups[domain]) { + domainGroups[domain] = []; + } + domainGroups[domain].push(tab.id); + } + } + + for (const groupId in groupIds) { + if (groupIds[groupId] != -1) { + let tabGroup = await browser.tabGroups.get(groupIds[groupId]); + + if (tabGroup.title in domainGroups) { + const updatedGroup = await browser.tabs.group({ + tabIds: domainGroups[tabGroup.title], + groupId: tabGroup.id, + }); + + delete domainGroups[tabGroup.title]; + } + } + } + + for (const domain in domainGroups) { + const groupId = await browser.tabs.group({ + tabIds: domainGroups[domain], + }); + + await browser.tabGroups.update(groupId, { + title: domain, + color: colors[groupId % colors.length], + }); + } +} + +function toTitleCase(str) { + str = str.replace(/[-_]/g, " "); + return str.replace( + /\w(\S)*/g, + (text) => + text.charAt(0).toUpperCase() + text.substring(1).toLowerCase(), + ); +} |
