Merge pull request #273 from alibaba/feat/extension-control-curr-tab-group
feat(ext): initial controlled group
This commit is contained in:
@@ -52,7 +52,7 @@ export function handleTabControlMessage(
|
|||||||
.create({ url: payload.url, active: false })
|
.create({ url: payload.url, active: false })
|
||||||
.then((newTab) => {
|
.then((newTab) => {
|
||||||
debug('open_new_tab: success', newTab)
|
debug('open_new_tab: success', newTab)
|
||||||
sendResponse({ success: true, tabId: newTab.id, windowId: newTab.windowId })
|
sendResponse({ success: true, tabId: newTab.id })
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
sendResponse({ error: error instanceof Error ? error.message : String(error) })
|
sendResponse({ error: error instanceof Error ? error.message : String(error) })
|
||||||
@@ -63,7 +63,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, createProperties: { windowId: payload.windowId } })
|
.group({ tabIds: payload.tabIds })
|
||||||
.then((groupId) => {
|
.then((groupId) => {
|
||||||
debug('create_tab_group: success', groupId)
|
debug('create_tab_group: success', groupId)
|
||||||
sendResponse({ success: true, groupId })
|
sendResponse({ success: true, groupId })
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ export class TabsController extends EventTarget {
|
|||||||
private initialTabId: number | null = null
|
private initialTabId: number | null = null
|
||||||
private tabGroupId: number | null = null
|
private tabGroupId: number | null = null
|
||||||
private task: string = ''
|
private task: string = ''
|
||||||
private windowId: number | null = null
|
|
||||||
|
|
||||||
async init(task: string, includeInitialTab: boolean = true) {
|
async init(task: string, includeInitialTab: boolean = true) {
|
||||||
debug('init', task, includeInitialTab)
|
debug('init', task, includeInitialTab)
|
||||||
@@ -37,7 +36,6 @@ export class TabsController extends EventTarget {
|
|||||||
this.currentTabId = null
|
this.currentTabId = null
|
||||||
this.tabGroupId = null
|
this.tabGroupId = null
|
||||||
this.initialTabId = null
|
this.initialTabId = null
|
||||||
this.windowId = null
|
|
||||||
|
|
||||||
const result = await sendMessage({
|
const result = await sendMessage({
|
||||||
type: 'TAB_CONTROL',
|
type: 'TAB_CONTROL',
|
||||||
@@ -53,7 +51,6 @@ export class TabsController extends EventTarget {
|
|||||||
if (includeInitialTab) {
|
if (includeInitialTab) {
|
||||||
this.currentTabId = this.initialTabId
|
this.currentTabId = this.initialTabId
|
||||||
|
|
||||||
// update tab status immediately
|
|
||||||
const info = await sendMessage({
|
const info = await sendMessage({
|
||||||
type: 'TAB_CONTROL',
|
type: 'TAB_CONTROL',
|
||||||
action: 'get_tab_info',
|
action: 'get_tab_info',
|
||||||
@@ -67,6 +64,8 @@ export class TabsController extends EventTarget {
|
|||||||
title: info.title,
|
title: info.title,
|
||||||
status: info.status,
|
status: info.status,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
await this.createTabGroup([this.initialTabId])
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.updateCurrentTabId(this.currentTabId)
|
await this.updateCurrentTabId(this.currentTabId)
|
||||||
@@ -132,9 +131,6 @@ export class TabsController extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tabId = result.tabId as number
|
const tabId = result.tabId as number
|
||||||
const windowId = result.windowId as number
|
|
||||||
|
|
||||||
this.windowId = windowId
|
|
||||||
|
|
||||||
this.tabs.push({
|
this.tabs.push({
|
||||||
id: tabId,
|
id: tabId,
|
||||||
@@ -144,32 +140,7 @@ export class TabsController extends EventTarget {
|
|||||||
await this.switchToTab(tabId)
|
await this.switchToTab(tabId)
|
||||||
|
|
||||||
if (!this.tabGroupId) {
|
if (!this.tabGroupId) {
|
||||||
const result = await sendMessage({
|
await this.createTabGroup([tabId])
|
||||||
type: 'TAB_CONTROL',
|
|
||||||
action: 'create_tab_group',
|
|
||||||
payload: { tabIds: [tabId], windowId: this.windowId },
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!result.success) {
|
|
||||||
throw new Error(`Failed to create tab group: ${result.error}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const groupId = result.groupId as number
|
|
||||||
|
|
||||||
this.tabGroupId = groupId
|
|
||||||
|
|
||||||
await sendMessage({
|
|
||||||
type: 'TAB_CONTROL',
|
|
||||||
action: 'update_tab_group',
|
|
||||||
payload: {
|
|
||||||
groupId: this.tabGroupId,
|
|
||||||
properties: {
|
|
||||||
title: `PageAgent(${this.task})`,
|
|
||||||
color: randomColor(),
|
|
||||||
collapsed: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
await sendMessage({
|
await sendMessage({
|
||||||
type: 'TAB_CONTROL',
|
type: 'TAB_CONTROL',
|
||||||
@@ -230,6 +201,33 @@ export class TabsController extends EventTarget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async createTabGroup(tabIds: number[]) {
|
||||||
|
const result = await sendMessage({
|
||||||
|
type: 'TAB_CONTROL',
|
||||||
|
action: 'create_tab_group',
|
||||||
|
payload: { tabIds },
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!result?.success) {
|
||||||
|
throw new Error(`Failed to create tab group: ${result?.error}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.tabGroupId = result.groupId as number
|
||||||
|
|
||||||
|
await sendMessage({
|
||||||
|
type: 'TAB_CONTROL',
|
||||||
|
action: 'update_tab_group',
|
||||||
|
payload: {
|
||||||
|
groupId: this.tabGroupId,
|
||||||
|
properties: {
|
||||||
|
title: `PageAgent(${this.task})`,
|
||||||
|
color: randomColor(),
|
||||||
|
collapsed: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async updateCurrentTabId(tabId: number | null) {
|
async updateCurrentTabId(tabId: number | null) {
|
||||||
debug('updateCurrentTabId', tabId)
|
debug('updateCurrentTabId', tabId)
|
||||||
|
|
||||||
@@ -309,16 +307,7 @@ interface TabMeta {
|
|||||||
status?: 'loading' | 'unloaded' | 'complete'
|
status?: 'loading' | 'unloaded' | 'complete'
|
||||||
}
|
}
|
||||||
|
|
||||||
const TAB_GROUP_COLORS = [
|
const TAB_GROUP_COLORS = ['blue', 'red', 'yellow', 'green', 'pink', 'purple', 'cyan'] as const
|
||||||
'grey',
|
|
||||||
'blue',
|
|
||||||
'red',
|
|
||||||
'yellow',
|
|
||||||
'green',
|
|
||||||
'pink',
|
|
||||||
'purple',
|
|
||||||
'cyan',
|
|
||||||
] as const
|
|
||||||
|
|
||||||
type TabGroupColor = (typeof TAB_GROUP_COLORS)[number]
|
type TabGroupColor = (typeof TAB_GROUP_COLORS)[number]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user