()
| 613 | } |
| 614 | |
| 615 | async createPagesSnapshot(): Promise<Page[]> { |
| 616 | const {pages: allPages, isolatedContextNames} = await this.#getAllPages(); |
| 617 | |
| 618 | for (const page of allPages) { |
| 619 | let mcpPage = this.#mcpPages.get(page); |
| 620 | if (!mcpPage) { |
| 621 | mcpPage = new McpPage(page, this.#nextPageId++); |
| 622 | this.#mcpPages.set(page, mcpPage); |
| 623 | // We emulate a focused page for all pages to support multi-agent workflows. |
| 624 | void page.emulateFocusedPage(true).catch(error => { |
| 625 | this.logger?.('Error turning on focused page emulation', error); |
| 626 | }); |
| 627 | } |
| 628 | mcpPage.isolatedContextName = isolatedContextNames.get(page); |
| 629 | } |
| 630 | |
| 631 | // Prune orphaned #mcpPages entries (pages that no longer exist). |
| 632 | const currentPages = new Set(allPages); |
| 633 | for (const [page, mcpPage] of this.#mcpPages) { |
| 634 | if (!currentPages.has(page)) { |
| 635 | mcpPage.dispose(); |
| 636 | this.#mcpPages.delete(page); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | this.#pages = allPages.filter(page => { |
| 641 | return ( |
| 642 | this.#options.experimentalDevToolsDebugging || |
| 643 | !page.url().startsWith('devtools://') |
| 644 | ); |
| 645 | }); |
| 646 | |
| 647 | if ( |
| 648 | (!this.#selectedPage || |
| 649 | this.#pages.indexOf(this.#selectedPage.pptrPage) === -1) && |
| 650 | this.#pages[0] |
| 651 | ) { |
| 652 | this.selectPage(this.#getMcpPage(this.#pages[0])); |
| 653 | } |
| 654 | |
| 655 | await this.detectOpenDevToolsWindows(); |
| 656 | |
| 657 | return this.#pages; |
| 658 | } |
| 659 | |
| 660 | async #getAllPages(): Promise<{ |
| 661 | pages: Page[]; |
no test coverage detected