From 443168b3fa5ad73cc7dfe1b74034000c389a906d Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Sun, 3 Nov 2024 01:09:53 +0530 Subject: [PATCH] feat: pass options for headless mode --- .../classes/RemoteBrowser.ts | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/server/src/browser-management/classes/RemoteBrowser.ts b/server/src/browser-management/classes/RemoteBrowser.ts index 5f07190f..fd7b5a01 100644 --- a/server/src/browser-management/classes/RemoteBrowser.ts +++ b/server/src/browser-management/classes/RemoteBrowser.ts @@ -92,7 +92,34 @@ export class RemoteBrowser { * @returns {Promise} */ public initialize = async (options: RemoteBrowserOptions, userId: string): Promise => { - this.browser = (await options.browser.launch(options.launchOptions)); + const launchOptions = { + headless: true, + proxy: options.launchOptions?.proxy, + chromiumSandbox: false, + args: [ + '--no-sandbox', + '--disable-setuid-sandbox', + '--headless=new', + '--disable-gpu', + '--disable-dev-shm-usage', + '--disable-software-rasterizer', + '--in-process-gpu', + '--disable-infobars', + '--single-process', + '--no-zygote', + '--disable-notifications', + '--disable-extensions', + '--disable-background-timer-throttling', + ...(options.launchOptions?.args || []) + ], + env: { + ...process.env, + CHROMIUM_FLAGS: '--disable-gpu --no-sandbox --headless=new' + } + }; + console.log('Launch options before:', options.launchOptions); + this.browser = (await options.browser.launch(launchOptions)); + console.log('Launch options after:', options.launchOptions) const proxyConfig = await getDecryptedProxyConfig(userId); let proxyOptions: { server: string, username?: string, password?: string } = { server: '' }; if (proxyConfig.proxy_url) { @@ -107,6 +134,16 @@ export class RemoteBrowser { const contextOptions: any = { viewport: { height: 400, width: 900 }, // recordVideo: { dir: 'videos/' } + // Force reduced motion to prevent animation issues + reducedMotion: 'reduce', + // Force JavaScript to be enabled + javaScriptEnabled: true, + // Set a reasonable timeout + timeout: 50000, + // Disable hardware acceleration + forcedColors: 'none', + isMobile: false, + hasTouch: false }; if (proxyOptions.server) { @@ -116,9 +153,17 @@ export class RemoteBrowser { password: proxyOptions.password ? proxyOptions.password : undefined, }; } + const browserUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.38 Safari/537.36"; + + contextOptions.userAgent = browserUserAgent; this.context = await this.browser.newContext(contextOptions); + console.log(`Context from initialize: ${JSON.stringify(this.context)}`) this.currentPage = await this.context.newPage(); + console.log(`CPage from initialize: ${JSON.stringify(this.currentPage)}`) + // await this.currentPage.setExtraHTTPHeaders({ + // 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' + // }); const blocker = await PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch); await blocker.enableBlockingInPage(this.currentPage); this.client = await this.currentPage.context().newCDPSession(this.currentPage); @@ -331,6 +376,9 @@ export class RemoteBrowser { await this.stopScreencast(); const newPage = options ? await this.browser?.newPage(options) : await this.browser?.newPage(); + await newPage?.setExtraHTTPHeaders({ + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' + }); await this.currentPage?.close(); this.currentPage = newPage;