-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBrowserInstance.cs
51 lines (48 loc) · 1.87 KB
/
BrowserInstance.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using crdebug.Exceptions;
using crdebug.RemoteTypes;
using Newtonsoft.Json;
namespace crdebug {
public class BrowserInstance {
public readonly IPAddress Address;
public readonly int Port;
/// <summary>
/// Represents a running instance of Chrome with the remote debugging protocol enabled.
/// </summary>
/// <param name="address"></param>
/// <param name="port"></param>
public BrowserInstance (IPAddress address, int port) {
Address = address;
Port = port;
}
/// <summary>
/// Enumerates the tabs currently available for debugging.
/// </summary>
/// <returns>A list of TabInfo objects representing open tabs. These can be used to connect to a tab.</returns>
public async Task<TabInfo[]> EnumerateTabs () {
var wc = new WebClient();
string tabInfoJson = null;
try {
tabInfoJson = await wc.DownloadStringTaskAsync($"http://{Address}:{Port}/json/list");
} catch (Exception exc) {
throw new ChromeConnectException(
"Failed to enumerate tabs. Ensure that chrome remote debugging is enabled and the specified address and port are correct.", exc
);
}
if (tabInfoJson != null) {
try {
return JsonConvert.DeserializeObject<TabInfo[]>(tabInfoJson);
} catch (Exception exc) {
throw new ChromeConnectException("Failed to decode the JSON from the tabs list.", exc);
}
} else {
throw new ChromeConnectException("No tab list was returned by the server.");
}
}
}
}