77
88namespace PuppeteerSharp
99{
10+ /// <summary>
11+ /// JSHandle represents an in-page JavaScript object. JSHandles can be created with the <see cref="Page.EvaluateExpressionHandleAsync(string)"/> and <see cref="Page.EvaluateFunctionHandleAsync(string, object[])"/> methods.
12+ /// </summary>
1013 public class JSHandle
1114 {
12- private ExecutionContext _context ;
13- protected readonly Session _client ;
14- protected readonly ILogger _logger ;
15-
16- public JSHandle ( ExecutionContext context , Session client , object remoteObject )
15+ internal JSHandle ( ExecutionContext context , Session client , object remoteObject )
1716 {
18- _context = context ;
19- _client = client ;
20- _logger = _client . Connection . LoggerFactory . CreateLogger ( this . GetType ( ) ) ;
17+ ExecutionContext = context ;
18+ Client = client ;
19+ Logger = Client . Connection . LoggerFactory . CreateLogger ( this . GetType ( ) ) ;
2120 RemoteObject = remoteObject ;
2221 }
2322
24- public ExecutionContext ExecutionContext => _context ;
25- public bool Disposed { get ; set ; }
26- public dynamic RemoteObject { get ; internal set ; }
23+ /// <summary>
24+ /// Gets the execution context.
25+ /// </summary>
26+ /// <value>The execution context.</value>
27+ public ExecutionContext ExecutionContext { get ; }
28+ /// <summary>
29+ /// Gets or sets a value indicating whether this <see cref="PuppeteerSharp.JSHandle"/> is disposed.
30+ /// </summary>
31+ /// <value><c>true</c> if disposed; otherwise, <c>false</c>.</value>
32+ public bool Disposed { get ; private set ; }
33+ /// <summary>
34+ /// Gets or sets the remote object.
35+ /// </summary>
36+ /// <value>The remote object.</value>
37+ public dynamic RemoteObject { get ; }
38+ /// <summary>
39+ /// Gets the client.
40+ /// </summary>
41+ /// <value>The client.</value>
42+ protected Session Client { get ; }
43+ /// <summary>
44+ /// Gets the logger.
45+ /// </summary>
46+ /// <value>The logger.</value>
47+ protected ILogger Logger { get ; }
2748
2849 /// <summary>
2950 /// Fetches a single property from the referenced object
@@ -58,7 +79,7 @@ public async Task<JSHandle> GetPropertyAsync(string propertyName)
5879 /// </example>
5980 public async Task < Dictionary < string , JSHandle > > GetPropertiesAsync ( )
6081 {
61- var response = await _client . SendAsync ( "Runtime.getProperties" , new
82+ var response = await Client . SendAsync ( "Runtime.getProperties" , new
6283 {
6384 objectId = RemoteObject . objectId . ToString ( ) ,
6485 ownProperties = true
@@ -67,8 +88,11 @@ public async Task<Dictionary<string, JSHandle>> GetPropertiesAsync()
6788 foreach ( var property in response . result )
6889 {
6990 if ( property . enumerable == null )
91+ {
7092 continue ;
71- result . Add ( property . name . ToString ( ) , _context . ObjectHandleFactory ( property . value ) ) ;
93+ }
94+
95+ result . Add ( property . name . ToString ( ) , ExecutionContext . ObjectHandleFactory ( property . value ) ) ;
7296 }
7397 return result ;
7498 }
@@ -94,19 +118,23 @@ public async Task<T> JsonValueAsync<T>()
94118 {
95119 if ( RemoteObject . objectId != null )
96120 {
97- dynamic response = await _client . SendAsync ( "Runtime.callFunctionOn" , new Dictionary < string , object > ( )
121+ dynamic response = await Client . SendAsync ( "Runtime.callFunctionOn" , new Dictionary < string , object >
98122 {
99- { "functionDeclaration" , "function() { return this; }" } ,
100- { "objectId" , RemoteObject . objectId } ,
101- { "returnByValue" , true } ,
102- { "awaitPromise" , true }
123+ [ "functionDeclaration" ] = "function() { return this; }" ,
124+ [ "objectId" ] = RemoteObject . objectId ,
125+ [ "returnByValue" ] = true ,
126+ [ "awaitPromise" ] = true
103127 } ) ;
104128 return ( T ) RemoteObjectHelper . ValueFromRemoteObject < T > ( response . result ) ;
105129 }
106130
107131 return ( T ) RemoteObjectHelper . ValueFromRemoteObject < T > ( RemoteObject ) ;
108132 }
109133
134+ /// <summary>
135+ /// Disposes the Handle. It will mark the JSHandle as disposed and release the <see cref="JSHandle.RemoteObject"/>
136+ /// </summary>
137+ /// <returns>The async.</returns>
110138 public async Task DisposeAsync ( )
111139 {
112140 if ( Disposed )
@@ -115,9 +143,10 @@ public async Task DisposeAsync()
115143 }
116144
117145 Disposed = true ;
118- await RemoteObjectHelper . ReleaseObject ( _client , RemoteObject , _logger ) ;
146+ await RemoteObjectHelper . ReleaseObject ( Client , RemoteObject , Logger ) ;
119147 }
120148
149+ /// <inheritdoc/>
121150 public override string ToString ( )
122151 {
123152 if ( ( ( JObject ) RemoteObject ) [ "objectId" ] != null )
@@ -132,13 +161,25 @@ public override string ToString()
132161 internal object FormatArgument ( ExecutionContext context )
133162 {
134163 if ( ExecutionContext != context )
164+ {
135165 throw new PuppeteerException ( "JSHandles can be evaluated only in the context they were created!" ) ;
166+ }
167+
136168 if ( Disposed )
169+ {
137170 throw new PuppeteerException ( "JSHandle is disposed!" ) ;
171+ }
172+
138173 if ( RemoteObject . unserializableValue != null )
174+ {
139175 return new { RemoteObject . unserializableValue } ;
176+ }
177+
140178 if ( RemoteObject . objectId == null )
179+ {
141180 return new { RemoteObject . value } ;
181+ }
182+
142183 return new { RemoteObject . objectId } ;
143184 }
144185 }
0 commit comments