From 628c249d45e6107cb5ae234d5c657bebbce8ecf2 Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Wed, 18 Mar 2026 19:36:02 +0800 Subject: [PATCH] fix(ext): init without initialTab throws error --- .../extension/src/agent/MultiPageAgent.ts | 1 + .../src/agent/RemotePageController.ts | 4 +--- .../extension/src/agent/TabsController.ts | 24 +++++++++++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/extension/src/agent/MultiPageAgent.ts b/packages/extension/src/agent/MultiPageAgent.ts index a0a2dfa..f2e7b0a 100644 --- a/packages/extension/src/agent/MultiPageAgent.ts +++ b/packages/extension/src/agent/MultiPageAgent.ts @@ -75,6 +75,7 @@ export class MultiPageAgent extends PageAgentCore { }, onBeforeStep: async (agent) => { + if (!tabsController.currentTabId) return // make sure the current tab is loaded before the step starts await tabsController.waitUntilTabLoaded(tabsController.currentTabId!) }, diff --git a/packages/extension/src/agent/RemotePageController.ts b/packages/extension/src/agent/RemotePageController.ts index 7cfbc98..3a35ddd 100644 --- a/packages/extension/src/agent/RemotePageController.ts +++ b/packages/extension/src/agent/RemotePageController.ts @@ -58,8 +58,6 @@ export class RemotePageController { } async getBrowserState(): Promise { - if (!this.currentTabId) throw new Error('tabsController not initialized.') - let browserState = {} as BrowserState debug('getBrowserState', this.currentTabId) @@ -178,7 +176,7 @@ interface DomActionReturn { /** * Check if a URL can run content scripts. */ -function isContentScriptAllowed(url: string | undefined): boolean { +export function isContentScriptAllowed(url: string | undefined): boolean { if (!url) return false const restrictedPatterns = [ diff --git a/packages/extension/src/agent/TabsController.ts b/packages/extension/src/agent/TabsController.ts index b782a88..46fabf6 100644 --- a/packages/extension/src/agent/TabsController.ts +++ b/packages/extension/src/agent/TabsController.ts @@ -1,3 +1,5 @@ +import { isContentScriptAllowed } from './RemotePageController' + const PREFIX = '[TabsController]' function debug(...messages: any[]) { @@ -49,23 +51,25 @@ export class TabsController extends EventTarget { } if (includeInitialTab) { - this.currentTabId = this.initialTabId - const info = await sendMessage({ type: 'TAB_CONTROL', action: 'get_tab_info', payload: { tabId: this.initialTabId }, }) - this.tabs.push({ - id: result.tabId, - isInitial: true, - url: info.url, - title: info.title, - status: info.status, - }) + if (isContentScriptAllowed(info.url)) { + this.currentTabId = this.initialTabId - await this.createTabGroup([this.initialTabId]) + this.tabs.push({ + id: result.tabId, + isInitial: true, + url: info.url, + title: info.title, + status: info.status, + }) + + await this.createTabGroup([this.initialTabId]) + } } await this.updateCurrentTabId(this.currentTabId)