@@ -10,11 +10,11 @@ import type {Frame, JSHandle, Page, WebWorker} from '../third_party/index.js';
1010
1111import { ToolCategory } from './categories.js' ;
1212import type { Context } from './ToolDefinition.js' ;
13- import { definePageTool } from './ToolDefinition.js' ;
13+ import { defineTool , pageIdSchema } from './ToolDefinition.js' ;
1414
1515export type Evaluatable = Page | Frame | WebWorker ;
1616
17- export const evaluateScript = definePageTool ( cliArgs => {
17+ export const evaluateScript = defineTool ( cliArgs => {
1818 return {
1919 name : 'evaluate_script' ,
2020 description : `Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON,
@@ -48,6 +48,7 @@ Example with arguments: \`(el) => {
4848 )
4949 . optional ( )
5050 . describe ( `An optional list of arguments to pass to the function.` ) ,
51+ ...( cliArgs ?. experimentalPageIdRouting ? pageIdSchema : { } ) ,
5152 ...( cliArgs ?. categoryExtensions
5253 ? {
5354 serviceWorkerId : zod
@@ -60,17 +61,22 @@ Example with arguments: \`(el) => {
6061 : { } ) ,
6162 } ,
6263 handler : async ( request , response , context ) => {
64+ const page : Page = cliArgs ?. experimentalPageIdRouting
65+ ? context . resolvePageById ( request . params . pageId )
66+ : context . getSelectedPage ( ) ;
67+
6368 const args : Array < JSHandle < unknown > > = [ ] ;
6469 try {
6570 const frames = new Set < Frame > ( ) ;
6671 for ( const el of request . params . args ?? [ ] ) {
67- const handle = await context . getElementByUid ( el . uid , request . page ) ;
72+ const handle = await context . getElementByUid ( el . uid , page ) ;
6873 frames . add ( handle . frame ) ;
6974 args . push ( handle ) ;
7075 }
7176
7277 const evaluatable = await getEvaluatable (
7378 context ,
79+ page ,
7480 frames ,
7581 cliArgs ?. categoryExtensions ,
7682 request . params . serviceWorkerId as string | undefined ,
@@ -103,18 +109,20 @@ Example with arguments: \`(el) => {
103109
104110const getEvaluatable = async (
105111 context : Context ,
112+ page : Page ,
106113 frames : Set < Frame > ,
107114 enableExtensions ?: boolean ,
108115 serviceWorkerId ?: string ,
109116) : Promise < Evaluatable > => {
110117 if ( enableExtensions && serviceWorkerId ) {
111118 return getWebWorker ( context , serviceWorkerId ) ;
112119 }
113- return getPageOrFrame ( context , frames ) ;
120+ return getPageOrFrame ( context , page , frames ) ;
114121} ;
115122
116123const getPageOrFrame = async (
117124 context : Context ,
125+ page : Page ,
118126 frames : Set < Frame > ,
119127) : Promise < Page | Frame > => {
120128 let pageOrFrame : Page | Frame ;
@@ -124,7 +132,7 @@ const getPageOrFrame = async (
124132 "Elements from different frames can't be evaluated together." ,
125133 ) ;
126134 } else {
127- pageOrFrame = [ ...frames . values ( ) ] [ 0 ] ?? context . getSelectedPage ( ) ;
135+ pageOrFrame = [ ...frames . values ( ) ] [ 0 ] ?? page ;
128136 }
129137
130138 return pageOrFrame ;
0 commit comments