@@ -9,7 +9,7 @@ import {zod} from '../third_party/index.js';
99import type { Frame , JSHandle , Page , WebWorker } from '../third_party/index.js' ;
1010
1111import { ToolCategory } from './categories.js' ;
12- import type { Context } from './ToolDefinition.js' ;
12+ import type { Context , Response } from './ToolDefinition.js' ;
1313import { defineTool , pageIdSchema } from './ToolDefinition.js' ;
1414
1515export type Evaluatable = Page | Frame | WebWorker ;
@@ -61,63 +61,77 @@ Example with arguments: \`(el) => {
6161 : { } ) ,
6262 } ,
6363 handler : async ( request , response , context ) => {
64+ const {
65+ serviceWorkerId,
66+ args : uidArgs ,
67+ function : fnString ,
68+ pageId,
69+ } = request . params ;
70+
71+ if ( cliArgs ?. categoryExtensions && serviceWorkerId ) {
72+ if ( uidArgs && uidArgs . length > 0 ) {
73+ throw new Error (
74+ 'args (element uids) cannot be used when evaluating in a service worker.' ,
75+ ) ;
76+ }
77+ if ( pageId ) {
78+ throw new Error ( 'specify either a pageId or a serviceWorkerId.' ) ;
79+ }
80+
81+ const worker = await getWebWorker ( context , serviceWorkerId ) ;
82+ await performEvaluation ( worker , fnString , [ ] , response , context ) ;
83+ return ;
84+ }
85+
6486 const page : Page = cliArgs ?. experimentalPageIdRouting
6587 ? context . resolvePageById ( request . params . pageId )
6688 : context . getSelectedPage ( ) ;
6789
6890 const args : Array < JSHandle < unknown > > = [ ] ;
6991 try {
7092 const frames = new Set < Frame > ( ) ;
71- for ( const el of request . params . args ?? [ ] ) {
93+ for ( const el of uidArgs ?? [ ] ) {
7294 const handle = await context . getElementByUid ( el . uid , page ) ;
7395 frames . add ( handle . frame ) ;
7496 args . push ( handle ) ;
7597 }
7698
77- const evaluatable = await getEvaluatable (
78- context ,
79- page ,
80- frames ,
81- cliArgs ?. categoryExtensions ,
82- request . params . serviceWorkerId as string | undefined ,
83- ) ;
84-
85- const fn = await evaluatable . evaluateHandle (
86- `(${ request . params . function } )` ,
87- ) ;
88- args . unshift ( fn ) ;
89-
90- await context . waitForEventsAfterAction ( async ( ) => {
91- const result = await evaluatable . evaluate (
92- async ( fn , ...args ) => {
93- // @ts -expect-error no types.
94- return JSON . stringify ( await fn ( ...args ) ) ;
95- } ,
96- ...args ,
97- ) ;
98- response . appendResponseLine ( 'Script ran on page and returned:' ) ;
99- response . appendResponseLine ( '```json' ) ;
100- response . appendResponseLine ( `${ result } ` ) ;
101- response . appendResponseLine ( '```' ) ;
102- } ) ;
99+ const evaluatable = await getPageOrFrame ( context , page , frames ) ;
100+
101+ await performEvaluation ( evaluatable , fnString , args , response , context ) ;
103102 } finally {
104103 void Promise . allSettled ( args . map ( arg => arg . dispose ( ) ) ) ;
105104 }
106105 } ,
107106 } ;
108107} ) ;
109108
110- const getEvaluatable = async (
109+ const performEvaluation = async (
110+ evaluatable : Evaluatable ,
111+ fnString : string ,
112+ args : Array < JSHandle < unknown > > ,
113+ response : Response ,
111114 context : Context ,
112- page : Page ,
113- frames : Set < Frame > ,
114- enableExtensions ?: boolean ,
115- serviceWorkerId ?: string ,
116- ) : Promise < Evaluatable > => {
117- if ( enableExtensions && serviceWorkerId ) {
118- return getWebWorker ( context , serviceWorkerId ) ;
115+ ) => {
116+ const fn = await evaluatable . evaluateHandle ( `(${ fnString } )` ) ;
117+ try {
118+ await context . waitForEventsAfterAction ( async ( ) => {
119+ const result = await evaluatable . evaluate (
120+ async ( fn , ...args ) => {
121+ // @ts -expect-error no types for function fn
122+ return JSON . stringify ( await fn ( ...args ) ) ;
123+ } ,
124+ fn ,
125+ ...args ,
126+ ) ;
127+ response . appendResponseLine ( 'Script ran on page and returned:' ) ;
128+ response . appendResponseLine ( '```json' ) ;
129+ response . appendResponseLine ( `${ result } ` ) ;
130+ response . appendResponseLine ( '```' ) ;
131+ } ) ;
132+ } finally {
133+ void fn . dispose ( ) ;
119134 }
120- return getPageOrFrame ( context , page , frames ) ;
121135} ;
122136
123137const getPageOrFrame = async (
0 commit comments