Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
group tabs by window
Browse files Browse the repository at this point in the history
  • Loading branch information
nohzafk committed Dec 10, 2023
1 parent 3f45ce2 commit fd7649f
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chrome.storage.local.get("types", (result) => {
}
});

const tabMap = new Map<string, number>();
const windowGroupMaps: { [key: number]: Map<string, number> } = {};

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
chrome.storage.local.get("types", (resultStorage) => {
Expand All @@ -31,12 +31,12 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
});

chrome.tabGroups.onUpdated.addListener((group) => {
if (!group.title) return;
const type = group.title;

if (tabMap.has(type)) return;

tabMap.set(type, group.id);
if (!windowGroupMaps.hasOwnProperty(group.windowId)) {
windowGroupMaps[group.windowId] = new Map<string, number>();
}
if (group.title) {
windowGroupMaps[group.windowId].set(group.title, group.id);
}
});

async function groupOneType(type: string, tabIds: number[]) {
Expand All @@ -49,28 +49,41 @@ async function groupOneType(type: string, tabIds: number[]) {

async function createGroupWithTitle(tabId: number, title: string) {
try {
const groupId = await chrome.tabs.group({ tabIds: [tabId] });
await chrome.tabGroups.update(groupId, { title });
tabMap.set(title, groupId);
chrome.tabs.get(tabId, async (tab) => {
if (tab.windowId) {
const groupId = await chrome.tabs.group({ tabIds: [tabId] });
await chrome.tabGroups.update(groupId, { title });
windowGroupMaps[tab.windowId].set(title, groupId);
}
});
} catch (error) {
console.error("Error creating tab group:", error);
throw error;
}
}

async function processTabAndGroup(tab: chrome.tabs.Tab, types: any) {
if (!tab.id) {
throw new Error("Tab ID is undefined!");
if (!tab.id || !tab.windowId) {
throw new Error("Tab ID or WindowID is undefined!");
}
const openAIKey = await getStorage<string>("openai_key");
if (!openAIKey) return;

const type = await handleOneTab(tab, types, openAIKey);

// Get or create proper tabMap for the window
if (!windowGroupMaps.hasOwnProperty(tab.windowId)) {
windowGroupMaps[tab.windowId] = new Map();
}
const tabMap = windowGroupMaps[tab.windowId];

// Query all groups and update tabMap accordingly
const allGroups = await chrome.tabGroups.query({});
allGroups.forEach(
(group) => group.title && tabMap.set(group.title, group.id)
(group) =>
group.windowId === tab.windowId &&
group.title &&
tabMap.set(group.title, group.id)
);

// Check if a group already exists for this type
Expand Down Expand Up @@ -129,3 +142,12 @@ async function handleTabUpdate(

chrome.tabs.onCreated.addListener(handleNewTab);
chrome.tabs.onUpdated.addListener(handleTabUpdate);
chrome.tabs.onDetached.addListener((tabId, detachInfo) => {
const windowId = detachInfo.oldWindowId;
if (
windowGroupMaps.hasOwnProperty(windowId) &&
!chrome.tabs.query({ windowId })
) {
delete windowGroupMaps[windowId];
}
});

0 comments on commit fd7649f

Please sign in to comment.