22using System . Collections . Generic ;
33using System . Threading . Tasks ;
44using System . Linq ;
5- using Newtonsoft . Json . Linq ;
65
76namespace PuppeteerSharp
87{
@@ -24,27 +23,63 @@ public ExecutionContext(Session client, ContextPayload contextPayload, Func<dyna
2423 public string FrameId { get ; internal set ; }
2524 public bool IsDefault { get ; internal set ; }
2625
27- public async Task < object > EvaluateExpressionAsync ( string script )
28- => await EvaluateExpressionAsync < object > ( script ) ;
29-
30- public async Task < T > EvaluateExpressionAsync < T > ( string script )
31- {
32- var handle = await EvaluateExpressionHandleAsync ( script ) ;
33- var result = await handle . JsonValue < T > ( ) ;
34- await handle . Dispose ( ) ;
35- return result ;
36- }
37-
38- public async Task < object > EvaluateFunctionAsync ( string script , params object [ ] args )
39- => await EvaluateFunctionAsync < object > ( script , args ) ;
40-
41- public async Task < T > EvaluateFunctionAsync < T > ( string script , params object [ ] args )
42- {
43- var handle = await EvaluateFunctionHandleAsync ( script , args ) ;
44- var result = await handle . JsonValue < T > ( ) ;
45- await handle . Dispose ( ) ;
46- return result ;
47- }
26+ /// <summary>
27+ /// Executes a script in browser context
28+ /// </summary>
29+ /// <param name="script">Script to be evaluated in browser context</param>
30+ /// <remarks>
31+ /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
32+ /// </remarks>
33+ /// <seealso cref="EvaluateFunctionAsync(string, object[])"/>
34+ /// <seealso cref="EvaluateExpressionHandleAsync(string)"/>
35+ /// <returns>Task which resolves to script return value</returns>
36+ public Task < object > EvaluateExpressionAsync ( string script )
37+ => EvaluateExpressionAsync < object > ( script ) ;
38+
39+ /// <summary>
40+ /// Executes a script in browser context
41+ /// </summary>
42+ /// <typeparam name="T">The type to deserialize the result to</typeparam>
43+ /// <param name="script">Script to be evaluated in browser context</param>
44+ /// <remarks>
45+ /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
46+ /// </remarks>
47+ /// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>
48+ /// <seealso cref="EvaluateExpressionHandleAsync(string)"/>
49+ /// <returns>Task which resolves to script return value</returns>
50+ public Task < T > EvaluateExpressionAsync < T > ( string script )
51+ => EvaluateAsync < T > ( EvaluateExpressionHandleAsync ( script ) ) ;
52+
53+ /// <summary>
54+ /// Executes a function in browser context
55+ /// </summary>
56+ /// <param name="script">Script to be evaluated in browser context</param>
57+ /// <param name="args">Arguments to pass to script</param>
58+ /// <remarks>
59+ /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
60+ /// <see cref="JSHandle"/> instances can be passed as arguments
61+ /// </remarks>
62+ /// <seealso cref="EvaluateExpressionAsync(string)"/>
63+ /// <seealso cref="EvaluateFunctionHandleAsync(string, object[])"/>
64+ /// <returns>Task which resolves to script return value</returns>
65+ public Task < object > EvaluateFunctionAsync ( string script , params object [ ] args )
66+ => EvaluateFunctionAsync < object > ( script , args ) ;
67+
68+ /// <summary>
69+ /// Executes a function in browser context
70+ /// </summary>
71+ /// <typeparam name="T">The type to deserialize the result to</typeparam>
72+ /// <param name="script">Script to be evaluated in browser context</param>
73+ /// <param name="args">Arguments to pass to script</param>
74+ /// <remarks>
75+ /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
76+ /// <see cref="JSHandle"/> instances can be passed as arguments
77+ /// </remarks>
78+ /// <seealso cref="EvaluateExpressionAsync{T}(string)"/>
79+ /// <seealso cref="EvaluateFunctionHandleAsync(string, object[])"/>
80+ /// <returns>Task which resolves to script return value</returns>
81+ public Task < T > EvaluateFunctionAsync < T > ( string script , params object [ ] args )
82+ => EvaluateAsync < T > ( EvaluateFunctionHandleAsync ( script , args ) ) ;
4883
4984 internal async Task < JSHandle > EvaluateExpressionHandleAsync ( string script )
5085 {
@@ -79,6 +114,16 @@ internal async Task<JSHandle> EvaluateFunctionHandleAsync(string script, params
79114 } ) ;
80115 }
81116
117+ private async Task < T > EvaluateAsync < T > ( Task < JSHandle > handleEvaluator )
118+ {
119+ var handle = await handleEvaluator ;
120+ var result = await handle . JsonValue < T > ( )
121+ . ContinueWith ( jsonTask => jsonTask . Exception != null ? default ( T ) : jsonTask . Result ) ;
122+
123+ await handle . Dispose ( ) ;
124+ return result ;
125+ }
126+
82127 private async Task < JSHandle > EvaluateHandleAsync ( string method , dynamic args )
83128 {
84129 dynamic response = await _client . SendAsync ( method , args ) ;
@@ -136,6 +181,5 @@ public async Task<dynamic> QueryObjects(JSHandle prototypeHandle)
136181
137182 return ObjectHandleFactory ( response . objects ) ;
138183 }
139-
140184 }
141- }
185+ }
0 commit comments