@@ -37,7 +37,11 @@ import {PredefinedNetworkConditions} from './third_party/index.js';
3737import { listPages } from './tools/pages.js' ;
3838import { takeSnapshot } from './tools/snapshot.js' ;
3939import { CLOSE_PAGE_ERROR } from './tools/ToolDefinition.js' ;
40- import type { Context , DevToolsData } from './tools/ToolDefinition.js' ;
40+ import type {
41+ Context ,
42+ DevToolsData ,
43+ ContextPage ,
44+ } from './tools/ToolDefinition.js' ;
4145import type { TraceResult } from './trace-processing/parse.js' ;
4246import type {
4347 EmulationSettings ,
@@ -117,7 +121,7 @@ export class McpContext implements Context {
117121 null ;
118122 #focusedPagePerContext = new Map < BrowserContext , Page > ( ) ;
119123
120- #requestPage?: Page ;
124+ #requestPage?: ContextPage ;
121125
122126 #nextPageId = 1 ;
123127
@@ -196,12 +200,12 @@ export class McpContext implements Context {
196200 // TODO: Refactor away mutable request state (e.g. per-request facade,
197201 // per-request context object, or another approach). Once resolved, the
198202 // global toolMutex could become per-BrowserContext for parallel execution.
199- setRequestPage ( page ?: Page ) : void {
203+ setRequestPage ( page ?: ContextPage ) : void {
200204 this . #requestPage = page ;
201205 }
202206
203207 #resolveTargetPage( ) : Page {
204- return this . #requestPage ?? this . getSelectedPage ( ) ;
208+ return this . #requestPage?. pptrPage ?? this . getSelectedPage ( ) ;
205209 }
206210
207211 resolveCdpRequestId ( cdpRequestId : string ) : number | undefined {
@@ -283,7 +287,7 @@ export class McpContext implements Context {
283287 async newPage (
284288 background ?: boolean ,
285289 isolatedContextName ?: string ,
286- ) : Promise < Page > {
290+ ) : Promise < ContextPage > {
287291 let page : Page ;
288292 if ( isolatedContextName !== undefined ) {
289293 let ctx = this . #isolatedContexts. get ( isolatedContextName ) ;
@@ -299,7 +303,7 @@ export class McpContext implements Context {
299303 this . selectPage ( page ) ;
300304 this . #networkCollector. addPage ( page ) ;
301305 this . #consoleCollector. addPage ( page ) ;
302- return page ;
306+ return this . #getMcpPage ( page ) ;
303307 }
304308 async closePage ( pageId : number ) : Promise < void > {
305309 if ( this . #pages. length === 1 ) {
@@ -489,7 +493,8 @@ export class McpContext implements Context {
489493 }
490494
491495 getDialog ( page ?: Page ) : Dialog | undefined {
492- const targetPage = page ?? this . #requestPage ?? this . #selectedPage;
496+ const targetPage =
497+ page ?? this . #requestPage?. pptrPage ?? this . #selectedPage;
493498 if ( ! targetPage ) {
494499 return undefined ;
495500 }
@@ -516,11 +521,17 @@ export class McpContext implements Context {
516521 return page ;
517522 }
518523
519- resolvePageById ( pageId ?: number ) : Page {
524+ getSelectedMcpPage ( ) : McpPage {
525+ const page = this . getSelectedPage ( ) ;
526+ return this . #getMcpPage( page ) ;
527+ }
528+
529+ resolvePageById ( pageId ?: number ) : McpPage {
520530 if ( pageId === undefined ) {
521- return this . getSelectedPage ( ) ;
531+ return this . getSelectedMcpPage ( ) ;
522532 }
523- return this . getPageById ( pageId ) ;
533+ const page = this . getPageById ( pageId ) ;
534+ return this . #getMcpPage( page ) ;
524535 }
525536
526537 getPageById ( pageId : number ) : Page {
@@ -551,7 +562,8 @@ export class McpContext implements Context {
551562 return this . #selectedPage === page ;
552563 }
553564
554- assertPageIsFocused ( page : Page ) : void {
565+ assertPageIsFocused ( pageToCheck : Page | ContextPage ) : void {
566+ const page = 'pptrPage' in pageToCheck ? pageToCheck . pptrPage : pageToCheck ;
555567 const ctx = page . browserContext ( ) ;
556568 const focused = this . #focusedPagePerContext. get ( ctx ) ;
557569 if ( focused && focused !== page ) {
@@ -564,7 +576,9 @@ export class McpContext implements Context {
564576 }
565577 }
566578
567- selectPage ( newPage : Page ) : void {
579+ selectPage ( pageToSelect : Page | ContextPage ) : void {
580+ const newPage =
581+ 'pptrPage' in pageToSelect ? pageToSelect . pptrPage : pageToSelect ;
568582 const ctx = newPage . browserContext ( ) ;
569583 const oldFocused = this . #focusedPagePerContext. get ( ctx ) ;
570584 if ( oldFocused && oldFocused !== newPage && ! oldFocused . isClosed ( ) ) {
0 commit comments