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

Commit

Permalink
Merge remote-tracking branch 'fork/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Zhaoyi committed Dec 18, 2023
2 parents 834f48f + f60a06b commit 74e74ba
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Color, DEFAULT_GROUP, DEFAULT_PROMPT } from "./const";
import { handleOneTab } from "./services";
import { getRootDomain, getStorage, setStorage } from "./utils";
import {
getRootDomain,
getStorage,
setStorage,
tabGroupMap,
createdManualType,
updatedManualType,
curryFilterManualGroups,
} from "./utils";

chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
Expand Down Expand Up @@ -31,16 +39,23 @@ const windowGroupMaps: { [key: number]: Map<string, number> } = {};
const tabMap: { [key: number]: chrome.tabs.Tab } = {};

chrome.runtime.onMessage.addListener((message) => {
chrome.storage.local.get("types", (resultStorage) => {
chrome.storage.local.get("colors", (resultColors) => {
chrome.storage.local.get("types", async (resultStorage) => {
chrome.storage.local.get("colors", async (resultColors) => {
if (resultStorage.types) {
types = resultStorage.types;
if (resultColors.colors) colors = resultColors.colors;
const result = message.result;

const filterTabs = await curryFilterManualGroups();

types.forEach((_, i) => {
// Check if result[i] exists before accessing the 'type' property
if (result[i]) {
groupOneType(result[i].type, result[i].tabIds, colors[i]);
groupOneType(
result[i].type,
result[i].tabIds.filter(filterTabs),
colors[i]
);
result[i].tabIds.forEach((tabId: number) => {
if (tabId) {
chrome.tabs.get(tabId, (tab) => {
Expand All @@ -58,13 +73,23 @@ chrome.runtime.onMessage.addListener((message) => {
});
});

chrome.tabGroups.onUpdated.addListener((group) => {
chrome.tabGroups.onUpdated.addListener(async (group) => {
if (!windowGroupMaps.hasOwnProperty(group.windowId)) {
windowGroupMaps[group.windowId] = new Map<string, number>();
}
if (group.title) {
windowGroupMaps[group.windowId].set(group.title, group.id);
}

const types = await getStorage<string[]>("types");
// 更新types中的群组条目
if (types && types.length > 0) {
if (!tabGroupMap[group.id]) {
createdManualType(types, group);
} else {
updatedManualType(types, group);
}
}
});

async function groupOneType(type: string, tabIds: number[], color: Color) {
Expand Down
70 changes: 70 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,73 @@ export function getRootDomain(url: URL) {
}
return parts.slice(1).join(".");
}

export const getTabsFromGroup = async (
groupId: number
): Promise<chrome.tabs.Tab[]> => {
return new Promise((resolve) => {
chrome.tabs.query({ groupId }, (tabs) => {
resolve(tabs);
});
});
};

export const tabGroupMap: {
[key: number]: {
type: "manual" | "setting";
title: string;
};
} = {};

export const createdManualType = (
types: string[],
group: chrome.tabGroups.TabGroup
) => {
if (!group.title) return;
const hasCreatedType = types.find((type, index) => {
if (type === group.title) {
types[index] = group.title;
return true;
}
return false;
});
if (!hasCreatedType) {
types.push(group.title);
tabGroupMap[group.id] = { type: "manual", title: group.title };
setStorage<string[]>("types", types);
}
};

export const updatedManualType = (
types: string[],
group: chrome.tabGroups.TabGroup
) => {
if (!group.title) return;
const existType = types.findIndex(
(type) => type === tabGroupMap[group.id].title
);
if (existType) {
types.splice(existType, 1, group.title);
tabGroupMap[group.id] = { type: "manual", title: group.title };
setStorage<string[]>("types", types);
}
};

export const curryFilterManualGroups = async () => {
const manualGroups = Object.entries(tabGroupMap).filter(
([_groupId, group]) => group.type === "manual"
);
const manualGroupsTabs = (
await Promise.all(
manualGroups.map(async ([groupId, _group]) => {
const groupTabs = getTabsFromGroup(parseInt(groupId));
return groupTabs;
})
)
).flat();

// filter out tabs that are in manual groups
return (tabId: number) => {
return !manualGroupsTabs.map((tab) => tab.id).includes(tabId);
};
};

0 comments on commit 74e74ba

Please sign in to comment.