Skip to content

Commit 9ff3405

Browse files
authored
JSHandle docs (#300)
1 parent 0c753d5 commit 9ff3405

File tree

5 files changed

+68
-27
lines changed

5 files changed

+68
-27
lines changed

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<Stream> ScreenshotStreamAsync(ScreenshotOptions options)
8383
public async Task<byte[]> ScreenshotDataAsync(ScreenshotOptions options)
8484
{
8585
await ScrollIntoViewIfNeededAsync();
86-
dynamic metrics = await _client.SendAsync("Page.getLayoutMetrics") as JObject;
86+
dynamic metrics = await Client.SendAsync("Page.getLayoutMetrics") as JObject;
8787

8888
var boundingBox = await BoundingBoxAsync();
8989
if (boundingBox == null)
@@ -129,7 +129,7 @@ public async Task UploadFileAsync(params string[] filePaths)
129129
{
130130
var files = filePaths.Select(Path.GetFullPath).ToArray();
131131
var objectId = RemoteObject.objectId.ToString();
132-
await _client.SendAsync("DOM.setFileInputFiles", new { objectId, files });
132+
await Client.SendAsync("DOM.setFileInputFiles", new { objectId, files });
133133
}
134134

135135
/// <summary>
@@ -297,14 +297,14 @@ public async Task<BoundingBox> BoundingBoxAsync()
297297

298298
try
299299
{
300-
result = await _client.SendAsync("DOM.getBoxModel", new
300+
result = await Client.SendAsync("DOM.getBoxModel", new
301301
{
302302
objectId = RemoteObject.objectId.ToString()
303303
});
304304
}
305305
catch (PuppeteerException ex)
306306
{
307-
_logger.LogError(ex.Message);
307+
Logger.LogError(ex.Message);
308308
}
309309

310310
if (result == null)

lib/PuppeteerSharp/JSHandle.cs

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,44 @@
77

88
namespace 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
}

lib/PuppeteerSharp/Page.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private Page(Session client, Target target, FrameTree frameTree, bool ignoreHTTP
5858
Touchscreen = new Touchscreen(client, Keyboard);
5959
Tracing = new Tracing(client);
6060
Coverage = new Coverage(client);
61-
61+
6262
_frameManager = new FrameManager(client, frameTree, this);
6363
_networkManager = new NetworkManager(client, _frameManager);
6464
_emulationManager = new EmulationManager(client);
@@ -1525,7 +1525,7 @@ private async Task OnCertificateError(MessageEventArgs e)
15251525
{"action", "continue"}
15261526
});
15271527
}
1528-
catch(PuppeteerException ex)
1528+
catch (PuppeteerException ex)
15291529
{
15301530
_logger.LogError(ex.ToString());
15311531
}

lib/PuppeteerSharp/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Session(Connection connection, string targetId, string sessionId)
2727

2828
#region Properties
2929
public string TargetId { get; }
30-
public string SessionId { get; private set; }
30+
public string SessionId { get; }
3131
public Connection Connection { get; private set; }
3232
public event EventHandler<MessageEventArgs> MessageReceived;
3333
public event EventHandler<TracingCompleteEventArgs> TracingComplete;

lib/PuppeteerSharp/TargetInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public TargetInfo(dynamic targetInfo)
1818
public string Type { get; internal set; }
1919
public string Url { get; internal set; }
2020
public string TargetId { get; internal set; }
21-
public dynamic SourceObject { get; private set; }
21+
public dynamic SourceObject { get; }
2222
}
2323
}

0 commit comments

Comments
 (0)