From 6a263895be620be96fc92fc7da5f951ec2101c52 Mon Sep 17 00:00:00 2001 From: rtritto Date: Sat, 31 Aug 2024 15:47:05 +0200 Subject: [PATCH] Improve Disable Features flag --- lib/cjs/index.js | 41 ++++++++++++++++++++++++++--------------- lib/esm/index.mjs | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/lib/cjs/index.js b/lib/cjs/index.js index 673df35..5b2cc31 100644 --- a/lib/cjs/index.js +++ b/lib/cjs/index.js @@ -22,23 +22,34 @@ async function connect({ args = [], headless = false, customConfig = {}, proxy = } } + let chromeFlags + if (ignoreAllFlags === true) { + chromeFlags = [ + ...args, + ...((headless !== false) ? [`--headless=${headless}`] : []), + ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []) + ] + } else { + // Default flags: https://github.com/GoogleChrome/chrome-launcher/blob/main/src/flags.ts + const flags = Launcher.defaultFlags() + // Add AutomationControlled to "disable-features" flag + const indexDisableFeatures = flags.findIndex((flag) => flag.startsWith('--disable-features')) + flags[indexDisableFeatures] = `${flags[indexDisableFeatures]},AutomationControlled` + // Remove "disable-component-update" flag + const indexComponentUpdateFlag = flags.findIndex((flag) => flag.startsWith('--disable-component-update')) + flags.splice(indexComponentUpdateFlag, 1) + chromeFlags = [ + ...flags, + ...args, + ...((headless !== false) ? [`--headless=${headless}`] : []), + ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), + '--no-sandbox' + ] + } + const chrome = await launch({ ignoreDefaultFlags: true, - chromeFlags: [ - ...( - (ignoreAllFlags === true) - ? [ - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ] : [ - ...Launcher.defaultFlags().filter(item => !item.includes("--disable-features") && !item.includes("component-update")), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - '--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled', - "--no-sandbox" - ] - ), - ], + chromeFlags, ...customConfig }); diff --git a/lib/esm/index.mjs b/lib/esm/index.mjs index 7742271..e6b4956 100644 --- a/lib/esm/index.mjs +++ b/lib/esm/index.mjs @@ -21,23 +21,34 @@ export async function connect({ args = [], headless = false, customConfig = {}, } } + let chromeFlags + if (ignoreAllFlags === true) { + chromeFlags = [ + ...args, + ...((headless !== false) ? [`--headless=${headless}`] : []), + ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []) + ] + } else { + // Default flags: https://github.com/GoogleChrome/chrome-launcher/blob/main/src/flags.ts + const flags = Launcher.defaultFlags() + // Add AutomationControlled to "disable-features" flag + const indexDisableFeatures = flags.findIndex((flag) => flag.startsWith('--disable-features')) + flags[indexDisableFeatures] = `${flags[indexDisableFeatures]},AutomationControlled` + // Remove "disable-component-update" flag + const indexComponentUpdateFlag = flags.findIndex((flag) => flag.startsWith('--disable-component-update')) + flags.splice(indexComponentUpdateFlag, 1) + chromeFlags = [ + ...flags, + ...args, + ...((headless !== false) ? [`--headless=${headless}`] : []), + ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), + '--no-sandbox' + ] + } + const chrome = await launch({ ignoreDefaultFlags: true, - chromeFlags: [ - ...( - (ignoreAllFlags === true) - ? [ - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ] : [ - ...Launcher.defaultFlags().filter(item => !item.includes("--disable-features") && !item.includes("component-update")), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - '--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled', - "--no-sandbox" - ] - ), - ], + chromeFlags, ...customConfig }); let pextra = null