mirror of
https://github.com/XFox111/TabsAsideExtension.git
synced 2026-04-22 07:58:01 +03:00
b51dd6083f
* chore(deps): wxt 0.20.0 bump #134 * chore: 3.2.1 manifest bump
117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
import { getCollectionTitle } from "@/entrypoints/sidepanel/utils/getCollectionTitle";
|
|
import { CollectionItem, GroupItem, TabItem } from "@/models/CollectionModels";
|
|
import { settings } from "@/utils/settings";
|
|
|
|
export async function openCollection(collection: CollectionItem, targetWindow?: "current" | "new" | "incognito"): Promise<void>
|
|
{
|
|
if (targetWindow === "incognito" && !(await browser.extension.isAllowedIncognitoAccess()))
|
|
throw new Error("The extension doesn't have incognito permission");
|
|
|
|
const discard: boolean = await settings.dismissOnLoad.getValue();
|
|
|
|
await manageWindow(
|
|
async windowId =>
|
|
{
|
|
if (collection.items.some(i => i.type === "group"))
|
|
// Open tabs as regular, open groups as groups
|
|
await Promise.all(collection.items.map(async i =>
|
|
{
|
|
if (i.type === "tab")
|
|
await createTab(i.url, windowId, discard);
|
|
else
|
|
await createGroup(i, windowId, discard);
|
|
}));
|
|
|
|
else if (collection.color)
|
|
// Open collection as one big group
|
|
await createGroup({
|
|
type: "group",
|
|
color: collection.color,
|
|
title: getCollectionTitle(collection),
|
|
items: collection.items as TabItem[]
|
|
}, windowId);
|
|
|
|
else
|
|
// Open collection tabs as is
|
|
await Promise.all(collection.items.map(async i =>
|
|
await createTab((i as TabItem).url, windowId, discard)
|
|
));
|
|
},
|
|
(!targetWindow || targetWindow === "current") ?
|
|
undefined :
|
|
{ incognito: targetWindow === "incognito" }
|
|
);
|
|
}
|
|
|
|
export async function openGroup(group: GroupItem, newWindow: boolean = false): Promise<void>
|
|
{
|
|
await manageWindow(
|
|
windowId => createGroup(group, windowId),
|
|
newWindow ? {} : undefined
|
|
);
|
|
}
|
|
|
|
async function createGroup(group: GroupItem, windowId: number, discard?: boolean): Promise<void>
|
|
{
|
|
discard ??= await settings.dismissOnLoad.getValue();
|
|
const tabs: Browser.tabs.Tab[] = await Promise.all(group.items.map(async i =>
|
|
await createTab(i.url, windowId, discard, group.pinned)
|
|
));
|
|
|
|
// "Pinned" group is technically not a group, so not much else to do here
|
|
if (group.pinned === true)
|
|
return;
|
|
|
|
const groupId: number = await browser.tabs.group({
|
|
tabIds: tabs.filter(i => i.windowId === windowId).map(i => i.id!) as [number, ...number[]],
|
|
createProperties: { windowId }
|
|
});
|
|
|
|
await browser.tabGroups.update(groupId, {
|
|
title: group.title,
|
|
color: group.color
|
|
});
|
|
}
|
|
|
|
async function manageWindow(handle: (windowId: number) => Promise<void>, windowProps?: Browser.windows.CreateData): Promise<void>
|
|
{
|
|
const currentWindow: Browser.windows.Window = windowProps ?
|
|
(await browser.windows.create({ url: "about:blank", focused: false, ...windowProps }))! :
|
|
await browser.windows.getCurrent();
|
|
const windowId: number = currentWindow.id!;
|
|
|
|
await handle(windowId);
|
|
|
|
await browser.windows.update(windowId, { focused: true });
|
|
|
|
if (windowProps)
|
|
// Close "about:blank" tab
|
|
await browser.tabs.remove(currentWindow.tabs![0].id!);
|
|
}
|
|
|
|
async function createTab(url: string, windowId: number, discard: boolean, pinned?: boolean): Promise<Browser.tabs.Tab>
|
|
{
|
|
const tab = await browser.tabs.create({ url, windowId: windowId, active: false, pinned });
|
|
|
|
if (discard)
|
|
discardOnLoad(tab.id!);
|
|
|
|
return tab;
|
|
}
|
|
|
|
function discardOnLoad(tabId: number): void
|
|
{
|
|
const handleTabUpdated = (id: number, _: any, tab: Browser.tabs.Tab) =>
|
|
{
|
|
if (id !== tabId || !tab.url)
|
|
return;
|
|
|
|
browser.tabs.onUpdated.removeListener(handleTabUpdated);
|
|
|
|
if (!tab.active)
|
|
browser.tabs.discard(tabId);
|
|
};
|
|
|
|
browser.tabs.onUpdated.addListener(handleTabUpdated);
|
|
}
|