/
ExecutionContext.cs
269 lines (235 loc) · 11.6 KB
/
ExecutionContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Messaging;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PuppeteerSharp
{
/// <summary>
/// The class represents a context for JavaScript execution. Examples of JavaScript contexts are:
/// Each <see cref="Frame"/> has a separate <see cref="ExecutionContext"/>
/// All kind of web workers have their own contexts
/// </summary>
public class ExecutionContext
{
internal const string EvaluationScriptUrl = "__puppeteer_evaluation_script__";
private readonly string _evaluationScriptSuffix = $"//# sourceURL={EvaluationScriptUrl}";
private static readonly Regex _sourceUrlRegex = new Regex(@"^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$", RegexOptions.Multiline);
private readonly CDPSession _client;
private readonly int _contextId;
internal DOMWorld World { get; }
internal ExecutionContext(
CDPSession client,
ContextPayload contextPayload,
DOMWorld world)
{
_client = client;
_contextId = contextPayload.Id;
World = world;
}
/// <summary>
/// Frame associated with this execution context.
/// </summary>
/// <remarks>
/// NOTE Not every execution context is associated with a frame. For example, workers and extensions have execution contexts that are not associated with frames.
/// </remarks>
public Frame Frame => World?.Frame;
/// <summary>
/// Executes a script in browser context
/// </summary>
/// <param name="script">Script to be evaluated in browser context</param>
/// <remarks>
/// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
/// </remarks>
/// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>
/// <seealso cref="EvaluateExpressionHandleAsync(string)"/>
/// <returns>Task which resolves to script return value</returns>
public Task<JToken> EvaluateExpressionAsync(string script) => EvaluateExpressionAsync<JToken>(script);
/// <summary>
/// Executes a script in browser context
/// </summary>
/// <typeparam name="T">The type to deserialize the result to</typeparam>
/// <param name="script">Script to be evaluated in browser context</param>
/// <remarks>
/// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
/// </remarks>
/// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>
/// <seealso cref="EvaluateExpressionHandleAsync(string)"/>
/// <returns>Task which resolves to script return value</returns>
public Task<T> EvaluateExpressionAsync<T>(string script)
=> RemoteObjectTaskToObject<T>(EvaluateExpressionInternalAsync(true, script));
internal async Task<JSHandle> EvaluateExpressionHandleAsync(string script)
=> CreateJSHandle(await EvaluateExpressionInternalAsync(false, script).ConfigureAwait(false));
/// <summary>
/// Executes a function in browser context
/// </summary>
/// <param name="script">Script to be evaluated in browser context</param>
/// <param name="args">Arguments to pass to script</param>
/// <remarks>
/// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
/// <see cref="JSHandle"/> instances can be passed as arguments
/// </remarks>
/// <seealso cref="EvaluateExpressionAsync{T}(string)"/>
/// <returns>Task which resolves to script return value</returns>
public Task<JToken> EvaluateFunctionAsync(string script, params object[] args) => EvaluateFunctionAsync<JToken>(script, args);
/// <summary>
/// Executes a function in browser context
/// </summary>
/// <typeparam name="T">The type to deserialize the result to</typeparam>
/// <param name="script">Script to be evaluated in browser context</param>
/// <param name="args">Arguments to pass to script</param>
/// <remarks>
/// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
/// <see cref="JSHandle"/> instances can be passed as arguments
/// </remarks>
/// <seealso cref="EvaluateExpressionAsync{T}(string)"/>
/// <returns>Task which resolves to script return value</returns>
public Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)
=> RemoteObjectTaskToObject<T>(EvaluateFunctionInternalAsync(true, script, args));
internal async Task<JSHandle> EvaluateFunctionHandleAsync(string script, params object[] args)
=> CreateJSHandle(await EvaluateFunctionInternalAsync(false, script, args).ConfigureAwait(false));
/// <summary>
/// The method iterates JavaScript heap and finds all the objects with the given prototype.
/// </summary>
/// <returns>A task which resolves to a handle to an array of objects with this prototype.</returns>
/// <param name="prototypeHandle">A handle to the object prototype.</param>
public async Task<JSHandle> QueryObjectsAsync(JSHandle prototypeHandle)
{
if (prototypeHandle.Disposed)
{
throw new PuppeteerException("Prototype JSHandle is disposed!");
}
if (prototypeHandle.RemoteObject.ObjectId == null)
{
throw new PuppeteerException("Prototype JSHandle must not be referencing primitive value");
}
var response = await _client.SendAsync<RuntimeQueryObjectsResponse>("Runtime.queryObjects", new RuntimeQueryObjectsRequest
{
PrototypeObjectId = prototypeHandle.RemoteObject.ObjectId
}).ConfigureAwait(false);
return CreateJSHandle(response.Objects);
}
private async Task<T> RemoteObjectTaskToObject<T>(Task<RemoteObject> remote)
{
var response = await remote.ConfigureAwait(false);
return response == null ? default : (T)RemoteObjectHelper.ValueFromRemoteObject<T>(response);
}
private Task<RemoteObject> EvaluateExpressionInternalAsync(bool returnByValue, string script)
=> ExecuteEvaluationAsync("Runtime.evaluate", new Dictionary<string, object>
{
["expression"] = _sourceUrlRegex.IsMatch(script) ? script : $"{script}\n{_evaluationScriptSuffix}",
["contextId"] = _contextId,
["returnByValue"] = returnByValue,
["awaitPromise"] = true,
["userGesture"] = true
});
private Task<RemoteObject> EvaluateFunctionInternalAsync(bool returnByValue, string script, params object[] args)
=> ExecuteEvaluationAsync("Runtime.callFunctionOn", new RuntimeCallFunctionOnRequest
{
FunctionDeclaration = $"{script}\n{_evaluationScriptSuffix}\n",
ExecutionContextId = _contextId,
Arguments = args.Select(FormatArgument),
ReturnByValue = returnByValue,
AwaitPromise = true,
UserGesture = true
});
private async Task<RemoteObject> ExecuteEvaluationAsync(string method, object args)
{
try
{
var response = await _client.SendAsync<EvaluateHandleResponse>(method, args).ConfigureAwait(false);
if (response.ExceptionDetails != null)
{
throw new EvaluationFailedException("Evaluation failed: " +
GetExceptionMessage(response.ExceptionDetails));
}
return response.Result;
}
catch (MessageException ex)
{
if (ex.Message.Contains("Object reference chain is too long") ||
ex.Message.Contains("Object couldn't be returned by value"))
{
return default;
}
throw new EvaluationFailedException(ex.Message, ex);
}
}
internal JSHandle CreateJSHandle(RemoteObject remoteObject)
=> remoteObject.Subtype == RemoteObjectSubtype.Node && Frame != null
? new ElementHandle(this, _client, remoteObject)
: new JSHandle(this, _client, remoteObject);
private object FormatArgument(object arg)
{
switch (arg)
{
case BigInteger big:
return new { unserializableValue = $"{big}n" };
case int integer when integer == -0:
return new { unserializableValue = "-0" };
case double d:
if (double.IsPositiveInfinity(d))
{
return new { unserializableValue = "Infinity" };
}
if (double.IsNegativeInfinity(d))
{
return new { unserializableValue = "-Infinity" };
}
if (double.IsNaN(d))
{
return new { unserializableValue = "NaN" };
}
break;
case JSHandle objectHandle:
return objectHandle.FormatArgument(this);
}
return new RuntimeCallFunctionOnRequestArgument
{
Value = arg
};
}
private static string GetExceptionMessage(EvaluateExceptionResponseDetails exceptionDetails)
{
if (exceptionDetails.Exception != null)
{
return exceptionDetails.Exception.Description ?? exceptionDetails.Exception.Value;
}
var message = exceptionDetails.Text;
if (exceptionDetails.StackTrace != null)
{
foreach (var callframe in exceptionDetails.StackTrace.CallFrames)
{
var location = $"{callframe.Url}:{callframe.LineNumber}:{callframe.ColumnNumber}";
var functionName = string.IsNullOrEmpty(callframe.FunctionName) ? "<anonymous>" : callframe.FunctionName;
message += $"\n at ${functionName} (${location})";
}
}
return message;
}
internal async Task<ElementHandle> AdoptElementHandleASync(ElementHandle elementHandle)
{
if (elementHandle.ExecutionContext == this)
{
throw new PuppeteerException("Cannot adopt handle that already belongs to this execution context");
}
if (World == null)
{
throw new PuppeteerException("Cannot adopt handle without DOMWorld");
}
var nodeInfo = await _client.SendAsync<DomDescribeNodeResponse>("DOM.describeNode", new DomDescribeNodeRequest
{
ObjectId = elementHandle.RemoteObject.ObjectId
}).ConfigureAwait(false);
var obj = await _client.SendAsync<DomResolveNodeResponse>("DOM.resolveNode", new DomResolveNodeRequest
{
BackendNodeId = nodeInfo.Node.BackendNodeId,
ExecutionContextId = _contextId
}).ConfigureAwait(false);
return CreateJSHandle(obj.Object) as ElementHandle;
}
}
}