Skip to content

Instantly share code, notes, and snippets.

@glued
Last active January 21, 2022 19:20
Show Gist options
  • Save glued/a004dc040a69f9695d3911b2dd18fd25 to your computer and use it in GitHub Desktop.
Save glued/a004dc040a69f9695d3911b2dd18fd25 to your computer and use it in GitHub Desktop.
Tabbycat ~ Chrome extension to group tabs into tabGroups by domain
const URL_REGEX = /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/gim;
const GOOG = ".google.com";
const domainFromUrl = (url) => {
if (url.includes(GOOG)) {
const segments = url.split("/");
const googIdx = segments.findIndex((item) => item.includes(GOOG));
return segments[googIdx + 1];
}
return url
.match(URL_REGEX)[0]
?.replace("https://", "")
.replace("http://", "")
.replace("www.", "")
.replace(".com", "");
};
const getActiveTab = () => {
return chrome.tabs
.query({ active: true, currentWindow: true })
.then((activeList) => activeList[0].id);
};
const createTabGroup = async (title, tabList, activeTabId) => {
const tabIds = tabList.map((tab) => tab.id);
const collapsed = !tabIds.includes(activeTabId);
const groupId = await chrome.tabs.group({ tabIds });
await chrome.tabGroups.update(groupId, { title, collapsed });
await chrome.tabGroups.move(groupId, { index: 0 });
};
chrome.action.onClicked.addListener(async () => {
const activeTabId = await getActiveTab();
const tabs = await chrome.tabs.query({});
const gm = new Map();
const tabIds = tabs.map((tab) => tab.id);
await chrome.tabs.ungroup(tabIds);
for (let tab of tabs) {
const tabKey = domainFromUrl(tab.url);
const tabList = gm.has(tabKey) ? [...gm.get(tabKey), tab] : [tab];
gm.set(tabKey, tabList);
}
const sorted = [...gm.entries()].sort(([aTitle], [bTitle]) =>
bTitle.localeCompare(aTitle)
);
for (let [title, tabList] of sorted) {
if (tabList.length === 1) continue;
await createTabGroup(title, tabList, activeTabId);
}
});
{
"name": "Tabbycat",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "Click to group tabs"
},
"permissions": [
"activeTab", "tabs", "scripting","tabGroups"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment