/
Browser.cs
executable file
·323 lines (274 loc) · 11.9 KB
/
Browser.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Messaging;
namespace PuppeteerSharp
{
/// <summary>
/// Provides methods to interact with a browser in Chromium.
/// </summary>
/// <example>
/// An example of using a <see cref="Browser"/> to create a <see cref="Page"/>:
/// <code>
/// <![CDATA[
/// var browser = await Puppeteer.LaunchAsync(new LaunchOptions());
/// var page = await browser.NewPageAsync();
/// await page.GoToAsync("https://example.com");
/// await browser.CloseAsync();
/// ]]>
/// </code>
/// An example of disconnecting from and reconnecting to a <see cref="Browser"/>:
/// <code>
/// <![CDATA[
/// var browser = await Puppeteer.LaunchAsync(new LaunchOptions());
/// var browserWSEndpoint = browser.WebSocketEndpoint;
/// browser.Disconnect();
/// var browser2 = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = browserWSEndpoint });
/// await browser2.CloseAsync();
/// ]]>
/// </code>
/// </example>
public class Browser : IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="Browser"/> class.
/// </summary>
/// <param name="connection">The connection</param>
/// <param name="options">The browser options</param>
/// <param name="process">The chrome process</param>
/// <param name="closeCallBack">An async function called before closing</param>
public Browser(Connection connection, IBrowserOptions options, Process process, Func<Task> closeCallBack)
{
Process = process;
Connection = connection;
IgnoreHTTPSErrors = options.IgnoreHTTPSErrors;
AppMode = options.AppMode;
TargetsMap = new Dictionary<string, Target>();
ScreenshotTaskQueue = new TaskQueue();
Connection.Closed += (object sender, EventArgs e) => Disconnected?.Invoke(this, new EventArgs());
Connection.MessageReceived += Connect_MessageReceived;
_closeCallBack = closeCallBack;
_logger = Connection.LoggerFactory.CreateLogger<Browser>();
}
#region Private members
internal readonly Dictionary<string, Target> TargetsMap;
private readonly Func<Task> _closeCallBack;
private readonly ILogger<Browser> _logger;
#endregion
#region Properties
/// <summary>
///
/// </summary>
public event EventHandler Closed;
/// <summary>
/// Raised when puppeteer gets disconnected from the Chromium instance. This might happen because one of the following
/// - Chromium is closed or crashed
/// - <see cref="Disconnect"/> method was called
/// </summary>
public event EventHandler Disconnected;
/// <summary>
/// Raised when the url of a target changes
/// </summary>
public event EventHandler<TargetChangedArgs> TargetChanged;
/// <summary>
/// Raised when a target is created, for example when a new page is opened by <c>window.open</c> <see href="https://developer.mozilla.org/en-US/docs/Web/API/Window/open"/> or <see cref="NewPageAsync"/>.
/// </summary>
public event EventHandler<TargetChangedArgs> TargetCreated;
/// <summary>
/// Raised when a target is destroyed, for example when a page is closed
/// </summary>
public event EventHandler<TargetChangedArgs> TargetDestroyed;
/// <summary>
/// Gets the Browser websocket url
/// </summary>
/// <remarks>
/// Browser websocket endpoint which can be used as an argument to <see cref="Puppeteer.ConnectAsync(ConnectOptions, ILoggerFactory)"/>.
/// The format is <c>ws://${host}:${port}/devtools/browser/[id]</c>
/// You can find the <c>webSocketDebuggerUrl</c> from <c>http://${host}:${port}/json/version</c>.
/// Learn more about the devtools protocol <see href="https://chromedevtools.github.io/devtools-protocol"/>
/// and the browser endpoint <see href="https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target"/>
/// </remarks>
public string WebSocketEndpoint => Connection.Url;
/// <summary>
/// Gets the spawned browser process. Returns <c>null</c> if the browser instance was created with <see cref="Puppeteer.ConnectAsync(ConnectOptions, ILoggerFactory)"/> method.
/// </summary>
public Process Process { get; }
/// <summary>
/// Gets or Sets whether to ignore HTTPS errors during navigation
/// </summary>
public bool IgnoreHTTPSErrors { get; set; }
/// <summary>
/// Gets or Sets whether to use appMode or not
/// </summary>
public bool AppMode { get; set; }
/// <summary>
/// Gets a value indicating if the browser is closed
/// </summary>
public bool IsClosed { get; internal set; }
internal TaskQueue ScreenshotTaskQueue { get; set; }
internal Connection Connection { get; }
#endregion
#region Public Methods
/// <summary>
/// Creates a new page
/// </summary>
/// <returns>Task which resolves to a new <see cref="Page"/> object</returns>
public async Task<Page> NewPageAsync()
{
string targetId = (await Connection.SendAsync("Target.createTarget", new Dictionary<string, object>
{
["url"] = "about:blank"
}).ConfigureAwait(false)).targetId.ToString();
var target = TargetsMap[targetId];
await target.InitializedTask.ConfigureAwait(false);
return await target.PageAsync().ConfigureAwait(false);
}
/// <summary>
/// Returns An Array of all active targets
/// </summary>
/// <returns>An Array of all active targets</returns>
public Target[] Targets() => TargetsMap.Values.Where(target => target.IsInitialized).ToArray();
/// <summary>
/// Returns a Task which resolves to an array of all open pages.
/// </summary>
/// <returns>Task which resolves to an array of all open pages.</returns>
public async Task<Page[]> PagesAsync()
=> (await Task.WhenAll(Targets().Select(target => target.PageAsync())).ConfigureAwait(false)).Where(x => x != null).ToArray();
/// <summary>
/// Gets the browser's version
/// </summary>
/// <returns>For headless Chromium, this is similar to <c>HeadlessChrome/61.0.3153.0</c>. For non-headless, this is similar to <c>Chrome/61.0.3153.0</c></returns>
/// <remarks>
/// the format of <see cref="GetVersionAsync"/> might change with future releases of Chromium
/// </remarks>
public async Task<string> GetVersionAsync()
{
dynamic version = await Connection.SendAsync("Browser.getVersion").ConfigureAwait(false);
return version.product.ToString();
}
/// <summary>
/// Gets the browser's original user agent
/// </summary>
/// <returns>Task which resolves to the browser's original user agent</returns>
/// <remarks>
/// Pages can override browser user agent with <see cref="Page.SetUserAgentAsync(string)"/>
/// </remarks>
public async Task<string> GetUserAgentAsync()
{
dynamic version = await Connection.SendAsync("Browser.getVersion").ConfigureAwait(false);
return version.userAgent.ToString();
}
/// <summary>
/// Disconnects Puppeteer from the browser, but leaves the Chromium process running. After calling <see cref="Disconnect"/>, the browser object is considered disposed and cannot be used anymore
/// </summary>
public void Disconnect() => Connection.Dispose();
/// <summary>
/// Closes Chromium and all of its pages (if any were opened). The browser object itself is considered disposed and cannot be used anymore
/// </summary>
/// <returns>Task</returns>
public async Task CloseAsync()
{
if (IsClosed)
{
return;
}
IsClosed = true;
Connection.StopReading();
var closeTask = _closeCallBack();
if (closeTask != null)
{
await closeTask.ConfigureAwait(false);
}
Disconnect();
Closed?.Invoke(this, new EventArgs());
}
#endregion
#region Private Methods
internal void ChangeTarget(Target target) => TargetChanged?.Invoke(this, new TargetChangedArgs
{
Target = target
});
private async void Connect_MessageReceived(object sender, MessageEventArgs e)
{
switch (e.MessageID)
{
case "Target.targetCreated":
await CreateTargetAsync(e.MessageData.ToObject<TargetCreatedResponse>()).ConfigureAwait(false);
return;
case "Target.targetDestroyed":
await DestroyTargetAsync(e.MessageData.ToObject<TargetDestroyedResponse>()).ConfigureAwait(false);
return;
case "Target.targetInfoChanged":
ChangeTargetInfo(e.MessageData.ToObject<TargetCreatedResponse>());
return;
}
}
private void ChangeTargetInfo(TargetCreatedResponse e)
{
if (!TargetsMap.ContainsKey(e.TargetInfo.TargetId))
{
throw new InvalidTargetException("Target should exists before ChangeTargetInfo");
}
var target = TargetsMap[e.TargetInfo.TargetId];
target.TargetInfoChanged(e.TargetInfo);
}
private async Task DestroyTargetAsync(TargetDestroyedResponse e)
{
if (!TargetsMap.ContainsKey(e.TargetId))
{
throw new InvalidTargetException("Target should exists before DestroyTarget");
}
var target = TargetsMap[e.TargetId];
TargetsMap.Remove(e.TargetId);
target.CloseTaskWrapper.TrySetResult(true);
if (await target.InitializedTask.ConfigureAwait(false))
{
TargetDestroyed?.Invoke(this, new TargetChangedArgs
{
Target = target
});
}
}
private async Task CreateTargetAsync(TargetCreatedResponse e)
{
var target = new Target(
e.TargetInfo,
() => Connection.CreateSessionAsync(e.TargetInfo.TargetId),
this);
if (TargetsMap.ContainsKey(e.TargetInfo.TargetId))
{
_logger.LogError("Target should not exist before targetCreated");
}
TargetsMap[e.TargetInfo.TargetId] = target;
if (await target.InitializedTask.ConfigureAwait(false))
{
TargetCreated?.Invoke(this, new TargetChangedArgs
{
Target = target
});
}
}
internal static async Task<Browser> CreateAsync(
Connection connection,
IBrowserOptions options,
Process process,
Func<Task> closeCallBack)
{
var browser = new Browser(connection, options, process, closeCallBack);
await connection.SendAsync("Target.setDiscoverTargets", new
{
discover = true
}).ConfigureAwait(false);
return browser;
}
#endregion
#region IDisposable
/// <inheritdoc />
public void Dispose() => CloseAsync().GetAwaiter().GetResult();
#endregion
}
}