fix: auto-switch to externally opened tabs (#404)
Update `setupContextTracking` in `BrowserManager` to auto-switch `activePageIndex` to newly opened tabs and invalidate the CDP session accordingly. This mirrors what `newTab()` and `newWindow()` already do for explicitly created tabs, and aligns CLI behavior with how real browsers focus newly opened tabs. Fixes #384
This commit is contained in:
+12
-1
@@ -1278,7 +1278,7 @@ export class BrowserManager {
|
||||
|
||||
/**
|
||||
* Set up tracking for new pages in a context (for CDP connections and popups/new tabs)
|
||||
* This handles pages created externally (e.g., via target="_blank" links)
|
||||
* This handles pages created externally (e.g., via target="_blank" links, window.open)
|
||||
*/
|
||||
private setupContextTracking(context: BrowserContext): void {
|
||||
context.on('page', (page) => {
|
||||
@@ -1287,6 +1287,17 @@ export class BrowserManager {
|
||||
this.pages.push(page);
|
||||
this.setupPageTracking(page);
|
||||
}
|
||||
|
||||
// Auto-switch to the newly opened tab so subsequent commands target it.
|
||||
// For tabs created via newTab()/newWindow(), this is redundant (they set activePageIndex after),
|
||||
// but for externally opened tabs (window.open, target="_blank"), this ensures the active tab
|
||||
// stays in sync with the browser.
|
||||
const newIndex = this.pages.indexOf(page);
|
||||
if (newIndex !== -1 && newIndex !== this.activePageIndex) {
|
||||
this.activePageIndex = newIndex;
|
||||
// Invalidate CDP session since the active page changed
|
||||
this.invalidateCDPSession().catch(() => {});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user