📄
PlaywrightBrowserService.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
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Microsoft.Playwright; internal sealed class PlaywrightBrowserService(IOptions<MonitorOptions> options) : IHostedService, IAsyncDisposable { private IPlaywright? playwright; private IBrowser? browser; public async Task StartAsync(CancellationToken cancellationToken) { playwright = await Playwright.CreateAsync(); browser = await playwright.Chromium.LaunchAsync( new BrowserTypeLaunchOptions { Headless = options.Value.Headless, Args = [ "--disable-blink-features=AutomationControlled", // "new" headless mode has a much closer fingerprint to headed Chrome // than the legacy headless implementation, helping bypass bot detection. .. options.Value.Headless ? ["--headless=new"] : Array.Empty<string>(), ], } ); } public async Task StopAsync(CancellationToken cancellationToken) => await DisposeAsync(); public async Task<IPage> NewPageAsync() { var page = await browser!.NewPageAsync(); // Patch navigator.webdriver before any page script runs so Cloudflare sees undefined. await page.AddInitScriptAsync("Object.defineProperty(navigator,'webdriver',{get:()=>undefined})"); return page; } public async ValueTask DisposeAsync() { if (browser is not null) { await browser.DisposeAsync(); } playwright?.Dispose(); } }