-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPI.cs
659 lines (562 loc) · 26.6 KB
/
API.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using crdebug.RemoteTypes;
using crdebug.Exceptions;
namespace crdebug {
public partial class DebugClient {
public class APIInstance {
public class ObjectOrException<T> where T : RemoteObject {
public T result;
public ExceptionDetails exceptionDetails;
}
class EnableState {
public int Count;
public bool IsEnabled {
get {
return Count > 0;
}
}
}
class GetBoxModelResult {
public BoxModel model;
}
class NodeIds {
public int[] nodeIds;
}
class BoxedNode {
public Node node;
}
class Root {
public Node root;
}
class Screenshot {
public string data;
}
class ResponseBody {
public string body;
public bool base64Encoded;
}
struct GetPropertiesResult {
public PropertyDescriptor[] result;
public ExceptionDetails exceptionDetails;
}
public readonly Dictionary<string, Frame> Frames = new Dictionary<string, Frame>();
public readonly Dictionary<string, Request> Requests = new Dictionary<string, Request>();
public readonly Dictionary<string, Response> Responses = new Dictionary<string, Response>();
private readonly Dictionary<string, EnableState> EnableStates = new Dictionary<string, EnableState>();
public readonly DebugClient Client;
private Node CachedDocument = null;
public event Action<Frame> OnNavigated;
public event Action<string, Frame> OnUrlChanged;
public event Action<string> OnStartedLoading;
public event Action<Request> OnRequestStarted;
public event Action<Request, Response> OnRequestComplete;
public event Action OnLoaded, OnPageDestroyed, OnPageCreated;
public event Action<string, ScreencastFrameMetadata> OnScreencastFrame;
private int NextGroupId = 1;
public string MostRecentFrameBase64 { get; private set; }
public ScreencastFrameMetadata MostRecentFrameMetadata { get; private set; }
public bool IsScreencastStarted { get; private set; }
public APIInstance (DebugClient client) {
Client = client;
Client.OnEvent += Client_OnEvent;
}
private void Client_OnEvent (string method, Newtonsoft.Json.Linq.JToken args) {
Frame frame = null;
switch (method) {
case "Page.navigatedWithinDocument":
var newUrl = args["url"].ToString();
Frames.TryGetValue(args["frameId"].ToString(), out frame);
OnUrlChanged?.Invoke(newUrl, frame);
break;
case "Page.frameNavigated":
frame = args["frame"].ToObject<Frame>();
Frames[frame.id] = frame;
OnNavigated?.Invoke(frame);
break;
case "Runtime.executionContextsCleared":
OnPageDestroyed?.Invoke();
IsScreencastStarted = false;
break;
case "Page.frameStartedLoading":
OnStartedLoading?.Invoke(args["frameId"].ToString());
break;
case "Page.loadEventFired":
OnLoaded?.Invoke();
break;
case "Network.loadingFinished":
var reqId = args["requestId"].ToString();
if (Responses.TryGetValue(reqId, out Response resp))
OnRequestComplete?.Invoke(resp.Request, resp);
break;
case "Network.responseReceived":
reqId = args["requestId"].ToString();
if (Requests.TryGetValue(reqId, out Request req))
Requests.Remove(reqId);
resp = args["response"].ToObject<Response>();
resp.Request = req;
resp.requestId = reqId;
Responses[reqId] = resp;
// HACK: Chrome sucks and the response is not actually ready yet.
// OnRequestComplete?.Invoke(req, resp);
break;
case "Network.requestWillBeSent":
reqId = args["requestId"].ToString();
req = args.ToObject<Request>();
req.frameId = args["frameId"].ToString();
Frames.TryGetValue(req.frameId, out req.Frame);
req.requestId = reqId;
Requests[reqId] = req;
OnRequestStarted?.Invoke(req);
break;
case "Page.screencastFrame":
var sessionId = (int)args["sessionId"];
var t = Client.Send("Page.screencastFrameAck", new { sessionId });
MostRecentFrameBase64 = args["data"].ToString();
MostRecentFrameMetadata = args["metadata"].ToObject<ScreencastFrameMetadata>();
OnScreencastFrame?.Invoke(MostRecentFrameBase64, MostRecentFrameMetadata);
break;
}
}
private EnableState GetEnableState (string category) {
EnableState result;
if (!EnableStates.TryGetValue(category, out result))
EnableStates[category] = result = new EnableState();
return result;
}
public async Task Enable (string category) {
var s = GetEnableState(category);
var enabled = s.IsEnabled;
s.Count++;
if (enabled)
return;
await Client.Send($"{category}.enable");
}
public async Task Disable (string category) {
var s = GetEnableState(category);
var enabled = s.IsEnabled;
s.Count--;
if (enabled && !s.IsEnabled)
await Client.Send($"{category}.disable");
}
private void CheckEnabled (string category) {
var s = GetEnableState(category);
if (!s.IsEnabled)
throw new InvalidOperationException("DOM must be enabled");
}
public async Task<NodeId> NodeForLocation (int x, int y) {
CheckEnabled("DOM");
var result = await Client.SendAndGetResult<NodeId>("DOM.getNodeForLocation", new {
x, y
});
return result;
}
public async Task<Node> GetDocument (bool cached = true, int? depth = null) {
if (CachedDocument != null) {
if (CachedDocument.depth < (depth ?? Client.DefaultDescriptionDepth))
cached = false;
}
if (cached && CachedDocument != null)
return CachedDocument;
var result = await Client.SendAndGetResult<Root>("DOM.getDocument", new {
depth = (depth ?? Client.DefaultDescriptionDepth)
});
return (CachedDocument = result.root);
}
public async Task<Node> GetBody (bool cached = true) {
var doc = await GetDocument(cached);
var html = doc.children.Last();
var body = html.children.FirstOrDefault(node => node.localName.ToLowerInvariant() == "body");
return body;
}
public async Task<Node> DescribeNode (NodeId id, int? depth = null) {
if (!id)
throw new ArgumentNullException("id");
object p;
if (id.backendNodeId != null)
p = new { id.backendNodeId, depth = (depth ?? Client.DefaultDescriptionDepth) };
else if (id.nodeId != null)
p = new { id.nodeId, depth = (depth ?? Client.DefaultDescriptionDepth) };
else
throw new Exception("Neither backendNodeId or nodeId specified");
Node result;
var res = await Client.SendAndGetResultOrException<BoxedNode>("DOM.describeNode", p);
if (res.Exception != null) {
// var cre = res.Exception.SourceException as ChromeRemoteException;
// res.Exception.Throw();
result = default(Node);
} else {
result = res.Result.node;
}
// workaround for describeNode bug https://bugs.chromium.org/p/chromium/issues/detail?id=972441
if (result == null) {
result = default(Node);
} else {
if (result.nodeId == 0)
result.nodeId = id.nodeId ?? 0;
if (result.backendNodeId == 0)
result.backendNodeId = id.backendNodeId;
}
return result;
}
public async Task<List<Node>> DescribeNodes (IEnumerable<NodeId> ids, int? depth = null) {
var tasks = new Dictionary<NodeId, Task<BoxedNode>>();
foreach (var id in ids) {
if (!id)
throw new ArgumentNullException("ids");
object p;
if (id.backendNodeId != null)
p = new { id.backendNodeId, depth = (depth ?? Client.DefaultDescriptionDepth) };
else if (id.nodeId != null)
p = new { id.nodeId, depth = (depth ?? Client.DefaultDescriptionDepth) };
else
throw new Exception("Neither backendNodeId or nodeId specified");
tasks.Add(
id, Client.SendAndGetResult<BoxedNode>("DOM.describeNode", p)
);
}
var results = new List<Node>();
foreach (var id in ids) {
var task = tasks[id];
var result = (await task).node;
// workaround for describeNode bug https://bugs.chromium.org/p/chromium/issues/detail?id=972441
if (result.nodeId == 0)
result.nodeId = id.nodeId ?? 0;
if (result.backendNodeId == 0)
result.backendNodeId = id.backendNodeId ?? 0;
results.Add(result);
}
return results;
}
private async Task<(int? nodeId, int? backendNodeId)> GetParentNodeIds (NodeId? parentNodeId) {
if (parentNodeId != null)
return (parentNodeId.Value.nodeId, parentNodeId.Value.backendNodeId);
var doc = (await GetDocument());
return (doc.nodeId, doc.backendNodeId);
}
public async Task<NodeId> QuerySelector (string selector, NodeId? parentNodeId = null) {
var id = await GetParentNodeIds(parentNodeId);
object p;
if (id.nodeId != null)
p = new {
id.nodeId,
selector
};
else
throw new Exception("No parentNodeId to query inside of");
var result = await Client.SendAndGetResult<NodeId>("DOM.querySelector", p);
return result;
}
public async Task<List<NodeId>> QuerySelectorAll (string selector, NodeId? parentNodeId = null) {
var id = await GetParentNodeIds(parentNodeId);
object p;
if (id.nodeId != null)
p = new {
id.nodeId,
selector
};
else
throw new Exception("No parentNodeId to query inside of");
var result = await Client.SendAndGetResult<NodeIds>("DOM.querySelectorAll", p);
return result.nodeIds.Select(i => new NodeId(i, null)).ToList();
}
public async Task<Node> QueryAndDescribeSelector (string selector, NodeId? parentNodeId = null, int? depth = null) {
var id = await GetParentNodeIds(parentNodeId);
object p;
if (id.nodeId != null)
p = new {
id.nodeId,
selector
};
else
throw new Exception("No parentNodeId to query inside of");
var res = await Client.SendAndGetResultOrException<NodeId>("DOM.querySelector", p);
if (res.Exception != null)
return null;
if (!res.Result)
return null;
var result = await DescribeNode(res.Result, depth);
return result;
}
public async Task<List<Node>> QueryAndDescribeSelectorAll (string selector, NodeId? parentNodeId = null, int? depth = null) {
var id = await GetParentNodeIds(parentNodeId);
object p;
if (id.nodeId != null)
p = new {
id.nodeId,
selector
};
else
throw new Exception("No parentNodeId to query inside of");
var results = new List<Node>();
var nodes = await Client.SendAndGetResult<NodeIds>("DOM.querySelectorAll", p);
foreach (var nodeId in nodes.nodeIds) {
var node = await DescribeNode(new NodeId(nodeId, null), depth);
if (node == null)
continue;
results.Add(node);
}
return results;
}
public async Task<BoxModel> GetBoxModel (NodeId id) {
BoxModel model = default(BoxModel);
try {
var result = await Client.SendAndGetResultOrException<GetBoxModelResult>("DOM.getBoxModel", new {
id.nodeId
});
if (result.Exception != null) {
model.Exception = result.Exception;
} else if (result.Result != null) {
model = result.Result.model;
model.Id = id;
}
} catch (Exception exc) {
model.Exception = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(exc);
}
return model;
}
public async Task StartScreencast (
string format = "jpeg", int quality = 75,
int? maxWidth = null, int? maxHeight = null,
int everyNthFrame = 1
) {
IsScreencastStarted = true;
var dict = new Dictionary<string, object> {
{ "format", format },
{ "quality", quality },
{ "everyNthFrame", 1 }
};
if (maxWidth != null)
dict.Add("maxWidth", maxWidth.Value);
if (maxHeight != null)
dict.Add("maxHeight", maxHeight.Value);
await Client.Send(
"Page.startScreencast", dict
);
}
public async Task StopScreencast () {
await Client.Send("Page.stopScreencast");
IsScreencastStarted = false;
}
public async Task<byte[]> CaptureScreenshot (string format = "jpeg", int quality = 75, int? maxWidth = null, int? maxHeight = null) {
var dict = new Dictionary<string, object> {
{ "format", format },
{ "quality", quality }
};
if (maxWidth != null)
dict.Add("maxWidth", maxWidth.Value);
if (maxHeight != null)
dict.Add("maxHeight", maxHeight.Value);
var result = await Client.SendAndGetResult<Screenshot>("Page.captureScreenshot", dict);
return Convert.FromBase64String(result.data);
}
private async Task<ResponseBody> GetResponseBodyInternal (string requestId) {
var result = await Client.SendAndGetResultOrException<ResponseBody>("Network.getResponseBody", new {
requestId
});
if (result.Exception == null)
return result.Result;
var exc = result.Exception.SourceException as ChromeRemoteException;
if (exc.Code == -32000)
return null;
result.Exception.Throw();
return null;
}
public async Task<byte[]> GetResponseBody (string requestId) {
var result = await GetResponseBodyInternal(requestId);
if (result?.base64Encoded ?? false)
return Convert.FromBase64String(result.body);
else if (result != null)
return Encoding.UTF8.GetBytes(result.body);
else
return null;
}
public async Task<string> GetResponseBodyText (string requestId) {
var result = await GetResponseBodyInternal(requestId);
if (result?.base64Encoded ?? false)
return Encoding.UTF8.GetString(Convert.FromBase64String(result.body));
else
return result?.body;
}
public async Task<LayoutMetrics> GetLayoutMetrics () {
var result = Client.SendAndGetResult<LayoutMetrics>("Page.getLayoutMetrics");
var metrics = await result;
using (var dpr = await Evaluate("window.devicePixelRatio"))
metrics.devicePixelRatio = Convert.ToDouble(dpr.value ?? 1.0);
return metrics;
}
public async Task MouseMove (int x, int y) {
var p = new {
type = "mouseMoved",
x, y,
button = "none",
clickCount = 0
};
await Client.Send("Input.dispatchMouseEvent", p);
}
public async Task Click (int x, int y) {
var p = new {
type = "mousePressed",
x, y,
button = "left",
clickCount = 1
};
await Client.Send("Input.dispatchMouseEvent", p);
var p2 = new {
type = "mouseReleased",
x, y,
button = "left",
clickCount = 1
};
await Client.Send("Input.dispatchMouseEvent", p2);
}
public Task ClickRandomPoint (BoxModel box, Random rng) {
return Click(
rng.Next(box.ContentLeft, box.ContentRight),
rng.Next(box.ContentTop, box.ContentBottom)
);
}
public async Task<PageLoadEventArgs> WaitForPageLoad () {
CheckEnabled("Page");
var f = Client.AwaitEvent<PageLoadEventArgs>("Page.loadEventFired");
return await f;
}
public async Task Reload () {
await Client.Send("Page.reload");
}
public async Task<NavigateResult> Navigate (string url, string transitionType = "address_bar") {
CheckEnabled("Page");
var result = await Client.SendAndGetResult<NavigateResult>("Page.navigate", new {
url, transitionType
});
return result;
}
internal void TryAnnotateWithClient (RemoteObject o) {
if (o == null)
return;
o.Client = Client;
}
public async Task<RemoteObject> Evaluate (string expression) {
var objectGroup = $"evaluate{NextGroupId++}";
var result = await Client.SendAndGetResult<ObjectOrException<RemoteObject>>("Runtime.evaluate", new {
expression
// objectGroup
});
TryAnnotateWithClient(result.exceptionDetails?.exception);
if (result.exceptionDetails != null)
throw new ChromeRemoteJSException(result.exceptionDetails);
result.result.Group = new RemoteObjectGroup(Client, objectGroup);
return result.result;
}
public async Task<RemoteObject> ExecuteScriptResource<T> (string name, Assembly assembly) where T : RemoteObject {
string source;
using (var s = assembly.GetManifestResourceStream(name))
using (var sr = new StreamReader(s, Encoding.UTF8))
source = sr.ReadToEnd();
var obj = await Evaluate(source);
return obj;
}
public Task<RemoteObject> CallFunctionOn (RemoteObject obj, string functionDeclaration, params object[] arguments) {
return CallFunctionOnWithFuture(obj, functionDeclaration, null, arguments);
}
public async Task<RemoteObject> CallFunctionOnWithFuture (RemoteObject obj, string functionDeclaration, IFuture future, params object[] arguments) {
if (obj == null)
throw new ArgumentNullException("obj");
var callArguments = new List<object>();
if (arguments != null)
foreach (var a in arguments) {
var ro = a as RemoteObject;
if (ro != null) {
if (ro.objectId != null)
callArguments.Add(new { ro.objectId });
else
callArguments.Add(new { ro.value });
} else {
callArguments.Add(new { value = a });
}
}
var objectGroup = $"callFunctionOn{NextGroupId++}";
var result = await Client.SendAndGetResult<ObjectOrException<RemoteObject>>("Runtime.callFunctionOn", new {
functionDeclaration,
obj.objectId,
arguments = callArguments.ToArray(),
userGesture = true,
objectGroup
});
TryAnnotateWithClient(result.exceptionDetails?.exception);
if (result.exceptionDetails != null)
throw new ChromeRemoteJSException(result.exceptionDetails);
result.result.Group = new RemoteObjectGroup(Client, objectGroup);
return result.result;
}
public Task<RemoteObject> CallMethodWithFuture (
RemoteObject @this, string methodName,
IFuture future, params object[] arguments
) {
var fn = "function () { return this." + methodName + ".apply(this, arguments); }";
return CallFunctionOnWithFuture(@this, fn, future, arguments);
}
public Task<RemoteObject> CallMethod (RemoteObject @this, string methodName, params object[] arguments) {
return CallMethodWithFuture(@this, methodName, null, arguments);
}
public async Task<RemoteObject> AwaitPromise (RemoteObject promise, Future<ObjectOrException<RemoteObject>> future = null) {
var result = await Client.SendAndGetResult<ObjectOrException<RemoteObject>>("Runtime.awaitPromise", new {
promiseObjectId = promise.objectId
}, future);
TryAnnotateWithClient(result.result);
TryAnnotateWithClient(result.exceptionDetails?.exception);
if (result.exceptionDetails != null)
throw new ChromeRemoteJSException(result.exceptionDetails);
return result.result;
}
public async Task HighlightNode (NodeId id) {
await Client.Send("DOM.highlightNode", new {
highlightConfig = new {
borderColor = new {
r = 64,
g = 200,
b = 255,
a = 0.8f
},
contentColor = new {
r = 64,
g = 200,
b = 255,
a = 0.33f
}
},
id.nodeId
});
}
public async Task<PropertyDescriptor[]> GetProperties (
RemoteObject obj, bool ownProperties = false, bool accessorPropertiesOnly = false
) {
var result = await Client.SendAndGetResult<GetPropertiesResult>(
"Runtime.getProperties", new {
obj.objectId, ownProperties, accessorPropertiesOnly
}
);
TryAnnotateWithClient(result.exceptionDetails?.exception);
if (result.exceptionDetails != null)
throw new ChromeRemoteJSException(result.exceptionDetails);
if (obj.Group == null) {
foreach (var p in result.result) {
TryAnnotateWithClient(p.value);
TryAnnotateWithClient(p.get);
TryAnnotateWithClient(p.set);
TryAnnotateWithClient(p.symbol);
}
}
return result.result;
}
public async Task HideHighlight () {
await Client.Send("DOM.hideHighlight");
}
}
}
}