Skip to content

Commit 41e8c2a

Browse files
authored
Implement browser context options (#2346)
1 parent dc757a4 commit 41e8c2a

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

lib/PuppeteerSharp/Browser.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,15 @@ public ITarget[] Targets()
148148
=> TargetManager.GetAvailableTargets().Values.ToArray();
149149

150150
/// <inheritdoc/>
151-
public async Task<IBrowserContext> CreateIncognitoBrowserContextAsync()
151+
public async Task<IBrowserContext> CreateIncognitoBrowserContextAsync(BrowserContextOptions options = null)
152152
{
153-
var response = await Connection.SendAsync<CreateBrowserContextResponse>("Target.createBrowserContext", null).ConfigureAwait(false);
153+
var response = await Connection.SendAsync<CreateBrowserContextResponse>(
154+
"Target.createBrowserContext",
155+
new TargetCreateBrowserContextRequest
156+
{
157+
ProxyServer = options?.ProxyServer ?? string.Empty,
158+
ProxyBypassList = string.Join(",", options?.ProxyBypassList ?? Array.Empty<string>()),
159+
}).ConfigureAwait(false);
154160
var context = new BrowserContext(Connection, this, response.BrowserContextId);
155161
_contexts.TryAdd(response.BrowserContextId, context);
156162
return context;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Logging;
9+
using PuppeteerSharp.Helpers;
10+
using PuppeteerSharp.Helpers.Json;
11+
using PuppeteerSharp.Messaging;
12+
using PuppeteerSharp.QueryHandlers;
13+
14+
namespace PuppeteerSharp
15+
{
16+
/// <summary>
17+
/// BrowserContext options.
18+
/// </summary>
19+
public class BrowserContextOptions
20+
{
21+
/// <summary>
22+
/// Proxy server with optional port to use for all requests.
23+
/// Username and password can be set in <see cref="IPage.AuthenticateAsync(Credentials)"/>.
24+
/// </summary>
25+
public string ProxyServer { get; set; }
26+
27+
/// <summary>
28+
/// Bypass the proxy for the given semi-colon-separated list of hosts.
29+
/// </summary>
30+
public string[] ProxyBypassList { get; set; }
31+
}
32+
}

lib/PuppeteerSharp/IBrowser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public interface IBrowser : IDisposable, IAsyncDisposable
7171
IBrowserContext DefaultContext { get; }
7272

7373
/// <summary>
74-
/// Dafault wait time in milliseconds. Defaults to 30 seconds.
74+
/// Default wait time in milliseconds. Defaults to 30 seconds.
7575
/// </summary>
7676
int DefaultWaitForTimeout { get; set; }
7777

@@ -127,6 +127,7 @@ public interface IBrowser : IDisposable, IAsyncDisposable
127127
/// <summary>
128128
/// Creates a new incognito browser context. This won't share cookies/cache with other browser contexts.
129129
/// </summary>
130+
/// <param name="options">Options.</param>
130131
/// <returns>Task which resolves to a new <see cref="IBrowserContext"/> object.</returns>
131132
/// <example>
132133
/// <code>
@@ -143,7 +144,7 @@ public interface IBrowser : IDisposable, IAsyncDisposable
143144
/// ]]>
144145
/// </code>
145146
/// </example>
146-
Task<IBrowserContext> CreateIncognitoBrowserContextAsync();
147+
Task<IBrowserContext> CreateIncognitoBrowserContextAsync(BrowserContextOptions options = null);
147148

148149
/// <summary>
149150
/// Disconnects Puppeteer from the browser, but leaves the process running. After calling <see cref="Disconnect"/>, the browser object is considered disposed and cannot be used anymore.
@@ -220,7 +221,7 @@ public interface IBrowser : IDisposable, IAsyncDisposable
220221
void RegisterCustomQueryHandler(string name, CustomQueryHandler queryHandler);
221222

222223
/// <summary>
223-
/// Unregisters a custom query handler.
224+
/// Unregister a custom query handler.
224225
/// </summary>
225226
/// <param name="name">The name of the query handler to unregistered.</param>
226227
void UnregisterCustomQueryHandler(string name);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace PuppeteerSharp.Messaging
2+
{
3+
internal class TargetCreateBrowserContextRequest
4+
{
5+
public string ProxyServer { get; set; }
6+
7+
public string ProxyBypassList { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)