From 362495098a964f42c6f754a4c276b7c561781263 Mon Sep 17 00:00:00 2001 From: HairlessVillager Date: Mon, 10 Feb 2025 13:14:18 +0000 Subject: [PATCH] =?UTF-8?q?add=20js/open=5Fnew=5Ftab.ts.=20=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E9=83=BD=E5=BA=94=E8=AF=A5=E6=9D=A5=E5=93=81?= =?UTF-8?q?=E9=89=B4=E4=B8=80=E4=B8=8B=E8=BF=99=E6=AE=B5=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E5=90=AB=E9=87=91=E9=87=8F=20```ts=20let=20tabs=20=3D?= =?UTF-8?q?=20window.tabs=20||=20[=20=20=20await=20chrome.tabs.create({=20?= =?UTF-8?q?=20=20=20=20url:=20url,=20=20=20=20=20windowId:=20windowId,=20?= =?UTF-8?q?=20=20}),=20];=20tabId=20=3D=20tabs[0].id=20as=20number;=20```?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: HairlessVillager --- js/open_new_tab.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 js/open_new_tab.ts diff --git a/js/open_new_tab.ts b/js/open_new_tab.ts new file mode 100644 index 0000000..4909a00 --- /dev/null +++ b/js/open_new_tab.ts @@ -0,0 +1,36 @@ +export async function open_new_tab( + url: string, + newWindow: boolean, + windowId?: number +): Promise { + let tabId; + if (newWindow) { + let window = await chrome.windows.create({ + type: 'normal', + state: 'maximized', + url: url, + } as any as chrome.windows.CreateData); + windowId = window.id as number; + let tabs = window.tabs || [ + await chrome.tabs.create({ + url: url, + windowId: windowId, + }), + ]; + tabId = tabs[0].id as number; + } else { + if (!windowId) { + const window = await chrome.windows.getCurrent(); + windowId = window.id; + } + let tab = await chrome.tabs.create({ + url: url, + windowId: windowId, + }); + tabId = tab.id as number; + } + let tab = await waitForTabComplete(tabId); + await sleep(200); + return tab; +} + -- Gitee