fix(ext): multi window errors

This commit is contained in:
Simon
2026-03-30 20:24:24 +08:00
parent 52edd78cd4
commit 312952ec41
2 changed files with 21 additions and 13 deletions

View File

@@ -19,11 +19,10 @@ export function handleTabControlMessage(
case 'get_active_tab': { case 'get_active_tab': {
debug('get_active_tab') debug('get_active_tab')
chrome.tabs chrome.tabs
.query({ active: true, currentWindow: true }) .query({ active: true })
.then((tabs) => { .then((tabs) => {
const tabId = tabs.length > 0 ? tabs[0].id || null : null debug('get_active_tab: success', tabs)
debug('get_active_tab: success', tabId) sendResponse({ success: true, tab: tabs[0] })
sendResponse({ success: true, tabId })
}) })
.catch((error) => { .catch((error) => {
sendResponse({ error: error instanceof Error ? error.message : String(error) }) sendResponse({ error: error instanceof Error ? error.message : String(error) })
@@ -62,7 +61,7 @@ export function handleTabControlMessage(
case 'create_tab_group': { case 'create_tab_group': {
debug('create_tab_group', payload) debug('create_tab_group', payload)
chrome.tabs chrome.tabs
.group({ tabIds: payload.tabIds }) .group({ tabIds: payload.tabIds, createProperties: { windowId: payload.windowId } })
.then((groupId) => { .then((groupId) => {
debug('create_tab_group: success', groupId) debug('create_tab_group: success', groupId)
sendResponse({ success: true, groupId }) sendResponse({ success: true, groupId })
@@ -114,9 +113,9 @@ export function handleTabControlMessage(
} }
case 'get_window_tabs': { case 'get_window_tabs': {
debug('get_window_tabs') debug('get_window_tabs', payload)
chrome.tabs chrome.tabs
.query({ currentWindow: true }) .query({ windowId: payload.windowId })
.then((tabs) => { .then((tabs) => {
sendResponse({ success: true, tabs }) sendResponse({ success: true, tabs })
}) })
@@ -133,6 +132,7 @@ export function handleTabControlMessage(
} }
export function setupTabChangeEvents() { export function setupTabChangeEvents() {
// @note It's normal to catch errors here before `TabsController.init()`
console.log('[TabsController.background] setupTabChangeEvents') console.log('[TabsController.background] setupTabChangeEvents')
chrome.tabs.onCreated.addListener((tab) => { chrome.tabs.onCreated.addListener((tab) => {

View File

@@ -23,6 +23,7 @@ function sendMessage(message: {
export class TabsController extends EventTarget { export class TabsController extends EventTarget {
currentTabId: number | null = null currentTabId: number | null = null
private windowId: number | null = null
private tabs: TabMeta[] = [] private tabs: TabMeta[] = []
private initialTabId: number | null = null private initialTabId: number | null = null
private tabGroupId: number | null = null private tabGroupId: number | null = null
@@ -34,27 +35,34 @@ export class TabsController extends EventTarget {
debug('init', task, options) debug('init', task, options)
this.task = task this.task = task
this.windowId = null
this.tabs = [] this.tabs = []
this.currentTabId = null this.currentTabId = null
this.tabGroupId = null this.tabGroupId = null
this.initialTabId = null this.initialTabId = null
this.experimentalIncludeAllTabs = experimentalIncludeAllTabs this.experimentalIncludeAllTabs = experimentalIncludeAllTabs
const result = await sendMessage({ const activeTabResult = await sendMessage({
type: 'TAB_CONTROL', type: 'TAB_CONTROL',
action: 'get_active_tab', action: 'get_active_tab',
}) })
this.initialTabId = result.tabId this.initialTabId = activeTabResult.tab?.id
this.windowId = activeTabResult.tab?.windowId
if (!this.initialTabId) { if (!this.initialTabId || !this.windowId) {
throw new Error('Failed to get initial tab ID') if (activeTabResult.error) {
throw new Error(activeTabResult.error)
} else {
throw new Error('Failed to get active tab')
}
} }
if (experimentalIncludeAllTabs) { if (experimentalIncludeAllTabs) {
const allTabs = await sendMessage({ const allTabs = await sendMessage({
type: 'TAB_CONTROL', type: 'TAB_CONTROL',
action: 'get_window_tabs', action: 'get_window_tabs',
payload: { windowId: this.windowId },
}) })
for (const tab of allTabs.tabs as chrome.tabs.Tab[]) { for (const tab of allTabs.tabs as chrome.tabs.Tab[]) {
if (tab.id && !tab.pinned && isContentScriptAllowed(tab.url)) { if (tab.id && !tab.pinned && isContentScriptAllowed(tab.url)) {
@@ -82,7 +90,7 @@ export class TabsController extends EventTarget {
this.currentTabId = this.initialTabId this.currentTabId = this.initialTabId
this.tabs.push({ this.tabs.push({
id: result.tabId, id: this.initialTabId,
isInitial: true, isInitial: true,
url: info.url, url: info.url,
title: info.title, title: info.title,
@@ -229,7 +237,7 @@ export class TabsController extends EventTarget {
const result = await sendMessage({ const result = await sendMessage({
type: 'TAB_CONTROL', type: 'TAB_CONTROL',
action: 'create_tab_group', action: 'create_tab_group',
payload: { tabIds }, payload: { tabIds, windowId: this.windowId },
}) })
if (!result?.success) { if (!result?.success) {