11using System ;
22using System . Collections . Generic ;
33using System . Threading . Tasks ;
4+ using System . Linq ;
5+ using Newtonsoft . Json . Linq ;
46
57namespace PuppeteerSharp
68{
@@ -22,44 +24,89 @@ public ExecutionContext(Session client, ContextPayload contextPayload, Func<dyna
2224 public string FrameId { get ; internal set ; }
2325 public bool IsDefault { get ; internal set ; }
2426
25- public async Task < dynamic > Evaluate ( string pageFunction , params object [ ] args )
27+ public async Task < dynamic > EvaluateExpressionAsync ( string script )
2628 {
27- var handle = await EvaluateHandleAsync ( pageFunction , args ) ;
29+ var handle = await EvaluateExpressionHandleAsync ( script ) ;
2830 dynamic result = await handle . JsonValue ( ) ;
2931 await handle . Dispose ( ) ;
3032 return result ;
3133 }
3234
33- internal async Task < JSHandle > EvaluateHandleAsync ( Func < object > pageFunction , object [ ] args )
35+ public async Task < T > EvaluateExpressionAsync < T > ( string script )
3436 {
35- throw new NotImplementedException ( ) ;
37+ var result = await EvaluateExpressionAsync ( script ) ;
38+ return ( ( JToken ) result ) . ToObject < T > ( ) ;
3639 }
3740
38- internal async Task < JSHandle > EvaluateHandleAsync ( string pageFunction , object [ ] args )
41+ public async Task < dynamic > EvaluateFunctionAsync ( string script , params object [ ] args )
3942 {
40- if ( ! string . IsNullOrEmpty ( pageFunction ) )
43+ var handle = await EvaluateFunctionHandleAsync ( script , args ) ;
44+ dynamic result = await handle . JsonValue ( ) ;
45+ await handle . Dispose ( ) ;
46+ return result ;
47+ }
48+
49+ public async Task < T > EvaluateFunctionAsync < T > ( string script , params object [ ] args )
50+ {
51+ var result = await EvaluateFunctionAsync ( script , args ) ;
52+ return ( ( JToken ) result ) . ToObject < T > ( ) ;
53+ }
54+
55+ internal async Task < JSHandle > EvaluateExpressionHandleAsync ( string script )
56+ {
57+ if ( string . IsNullOrEmpty ( script ) )
4158 {
42- dynamic remoteObject ;
59+ return null ;
60+ }
61+
62+ dynamic remoteObject ;
4363
44- try
64+ try
65+ {
66+ remoteObject = await _client . SendAsync ( "Runtime.evaluate" , new Dictionary < string , object > ( )
4567 {
46- remoteObject = await _client . SendAsync ( "Runtime.evaluate" , new Dictionary < string , object > ( )
47- {
48- [ "expression" ] = pageFunction ,
49- [ "contextId" ] = _contextId ,
50- [ "returnByValue" ] = false ,
51- [ "awaitPromise" ] = true
52- } ) ;
53-
54- return ObjectHandleFactory ( remoteObject . result ) ;
55- }
56- catch ( Exception ex )
68+ { "contextId" , _contextId } ,
69+ { "expression" , script } ,
70+ { "returnByValue" , false } ,
71+ { "awaitPromise" , true }
72+ } ) ;
73+
74+ return ObjectHandleFactory ( remoteObject . result ) ;
75+ }
76+ catch ( Exception ex )
77+ {
78+ throw new EvaluationFailedException ( "Evaluation Failed" , ex ) ;
79+ }
80+ }
81+
82+ internal async Task < JSHandle > EvaluateFunctionHandleAsync ( string script , object [ ] args )
83+ {
84+ if ( string . IsNullOrEmpty ( script ) )
85+ {
86+ return null ;
87+ }
88+
89+ dynamic response = await _client . SendAsync ( "Runtime.callFunctionOn" , new Dictionary < string , object > ( )
5790 {
58- throw new EvaluationFailedException ( "Evaluation Failed" , ex ) ;
59- }
91+ { "functionDeclaration" , script } ,
92+ { "executionContextId" , _contextId } ,
93+ { "arguments" , FormatArguments ( args ) } ,
94+ { "returnByValue" , false } ,
95+ { "awaitPromise" , true }
96+ } ) ;
97+
98+ if ( response . exceptionDetails != null )
99+ {
100+ throw new EvaluationFailedException ( "Evaluation failed: " +
101+ Helper . GetExceptionMessage ( response . exceptionDetails . ToObject < EvaluateExceptionDetails > ( ) ) ) ;
60102 }
61103
62- return null ;
104+ return ObjectHandleFactory ( response . result ) ;
105+ }
106+
107+ private object FormatArguments ( object [ ] args )
108+ {
109+ return args . Select ( o => new { value = o } ) ;
63110 }
64111
65112 public async Task < dynamic > QueryObjects ( JSHandle prototypeHandle )
0 commit comments