From 312952ec41be0c2fe02fd1d1a2f870249a09b629 Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Mon, 30 Mar 2026 20:24:24 +0800 Subject: [PATCH] fix(ext): multi window errors --- .../src/agent/TabsController.background.ts | 14 ++++++------- .../extension/src/agent/TabsController.ts | 20 +++++++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/extension/src/agent/TabsController.background.ts b/packages/extension/src/agent/TabsController.background.ts index 60ba212..79705d1 100644 --- a/packages/extension/src/agent/TabsController.background.ts +++ b/packages/extension/src/agent/TabsController.background.ts @@ -19,11 +19,10 @@ export function handleTabControlMessage( case 'get_active_tab': { debug('get_active_tab') chrome.tabs - .query({ active: true, currentWindow: true }) + .query({ active: true }) .then((tabs) => { - const tabId = tabs.length > 0 ? tabs[0].id || null : null - debug('get_active_tab: success', tabId) - sendResponse({ success: true, tabId }) + debug('get_active_tab: success', tabs) + sendResponse({ success: true, tab: tabs[0] }) }) .catch((error) => { sendResponse({ error: error instanceof Error ? error.message : String(error) }) @@ -62,7 +61,7 @@ export function handleTabControlMessage( case 'create_tab_group': { debug('create_tab_group', payload) chrome.tabs - .group({ tabIds: payload.tabIds }) + .group({ tabIds: payload.tabIds, createProperties: { windowId: payload.windowId } }) .then((groupId) => { debug('create_tab_group: success', groupId) sendResponse({ success: true, groupId }) @@ -114,9 +113,9 @@ export function handleTabControlMessage( } case 'get_window_tabs': { - debug('get_window_tabs') + debug('get_window_tabs', payload) chrome.tabs - .query({ currentWindow: true }) + .query({ windowId: payload.windowId }) .then((tabs) => { sendResponse({ success: true, tabs }) }) @@ -133,6 +132,7 @@ export function handleTabControlMessage( } export function setupTabChangeEvents() { + // @note It's normal to catch errors here before `TabsController.init()` console.log('[TabsController.background] setupTabChangeEvents') chrome.tabs.onCreated.addListener((tab) => { diff --git a/packages/extension/src/agent/TabsController.ts b/packages/extension/src/agent/TabsController.ts index 5492273..f8f04ea 100644 --- a/packages/extension/src/agent/TabsController.ts +++ b/packages/extension/src/agent/TabsController.ts @@ -23,6 +23,7 @@ function sendMessage(message: { export class TabsController extends EventTarget { currentTabId: number | null = null + private windowId: number | null = null private tabs: TabMeta[] = [] private initialTabId: number | null = null private tabGroupId: number | null = null @@ -34,27 +35,34 @@ export class TabsController extends EventTarget { debug('init', task, options) this.task = task + this.windowId = null this.tabs = [] this.currentTabId = null this.tabGroupId = null this.initialTabId = null this.experimentalIncludeAllTabs = experimentalIncludeAllTabs - const result = await sendMessage({ + const activeTabResult = await sendMessage({ type: 'TAB_CONTROL', action: 'get_active_tab', }) - this.initialTabId = result.tabId + this.initialTabId = activeTabResult.tab?.id + this.windowId = activeTabResult.tab?.windowId - if (!this.initialTabId) { - throw new Error('Failed to get initial tab ID') + if (!this.initialTabId || !this.windowId) { + if (activeTabResult.error) { + throw new Error(activeTabResult.error) + } else { + throw new Error('Failed to get active tab') + } } if (experimentalIncludeAllTabs) { const allTabs = await sendMessage({ type: 'TAB_CONTROL', action: 'get_window_tabs', + payload: { windowId: this.windowId }, }) for (const tab of allTabs.tabs as chrome.tabs.Tab[]) { if (tab.id && !tab.pinned && isContentScriptAllowed(tab.url)) { @@ -82,7 +90,7 @@ export class TabsController extends EventTarget { this.currentTabId = this.initialTabId this.tabs.push({ - id: result.tabId, + id: this.initialTabId, isInitial: true, url: info.url, title: info.title, @@ -229,7 +237,7 @@ export class TabsController extends EventTarget { const result = await sendMessage({ type: 'TAB_CONTROL', action: 'create_tab_group', - payload: { tabIds }, + payload: { tabIds, windowId: this.windowId }, }) if (!result?.success) {