fix(ext): init without initialTab throws error

This commit is contained in:
Simon
2026-03-18 19:36:02 +08:00
parent 61d598142d
commit 628c249d45
3 changed files with 16 additions and 13 deletions

View File

@@ -75,6 +75,7 @@ export class MultiPageAgent extends PageAgentCore {
}, },
onBeforeStep: async (agent) => { onBeforeStep: async (agent) => {
if (!tabsController.currentTabId) return
// make sure the current tab is loaded before the step starts // make sure the current tab is loaded before the step starts
await tabsController.waitUntilTabLoaded(tabsController.currentTabId!) await tabsController.waitUntilTabLoaded(tabsController.currentTabId!)
}, },

View File

@@ -58,8 +58,6 @@ export class RemotePageController {
} }
async getBrowserState(): Promise<BrowserState> { async getBrowserState(): Promise<BrowserState> {
if (!this.currentTabId) throw new Error('tabsController not initialized.')
let browserState = {} as BrowserState let browserState = {} as BrowserState
debug('getBrowserState', this.currentTabId) debug('getBrowserState', this.currentTabId)
@@ -178,7 +176,7 @@ interface DomActionReturn {
/** /**
* Check if a URL can run content scripts. * 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 if (!url) return false
const restrictedPatterns = [ const restrictedPatterns = [

View File

@@ -1,3 +1,5 @@
import { isContentScriptAllowed } from './RemotePageController'
const PREFIX = '[TabsController]' const PREFIX = '[TabsController]'
function debug(...messages: any[]) { function debug(...messages: any[]) {
@@ -49,23 +51,25 @@ export class TabsController extends EventTarget {
} }
if (includeInitialTab) { if (includeInitialTab) {
this.currentTabId = this.initialTabId
const info = await sendMessage({ const info = await sendMessage({
type: 'TAB_CONTROL', type: 'TAB_CONTROL',
action: 'get_tab_info', action: 'get_tab_info',
payload: { tabId: this.initialTabId }, payload: { tabId: this.initialTabId },
}) })
this.tabs.push({ if (isContentScriptAllowed(info.url)) {
id: result.tabId, this.currentTabId = this.initialTabId
isInitial: true,
url: info.url,
title: info.title,
status: info.status,
})
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) await this.updateCurrentTabId(this.currentTabId)