Skip to content

Commit d597325

Browse files
committed
Add launch arguments and requests headers in discovery browser
1 parent 174d5a4 commit d597325

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

src/lib/constants.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,66 @@ export default {
4646
EDGE_CHANNEL: 'msedge',
4747
WEBKIT: 'webkit',
4848

49+
// discovery browser launch arguments
50+
LAUNCH_ARGS: [
51+
// disable the translate popup and optimization downloads
52+
'--disable-features=Translate,OptimizationGuideModelDownloading',
53+
// disable several subsystems which run network requests in the background
54+
'--disable-background-networking',
55+
// disable task throttling of timer tasks from background pages
56+
'--disable-background-timer-throttling',
57+
// disable backgrounding renderer processes
58+
'--disable-renderer-backgrounding',
59+
// disable backgrounding renderers for occluded windows (reduce nondeterminism)
60+
'--disable-backgrounding-occluded-windows',
61+
// disable crash reporting
62+
'--disable-breakpad',
63+
// disable client side phishing detection
64+
'--disable-client-side-phishing-detection',
65+
// disable default component extensions with background pages for performance
66+
'--disable-component-extensions-with-background-pages',
67+
// disable installation of default apps on first run
68+
'--disable-default-apps',
69+
// work-around for environments where a small /dev/shm partition causes crashes
70+
'--disable-dev-shm-usage',
71+
// disable extensions
72+
'--disable-extensions',
73+
// disable hang monitor dialogs in renderer processes
74+
'--disable-hang-monitor',
75+
// disable inter-process communication flooding protection for javascript
76+
'--disable-ipc-flooding-protection',
77+
// disable web notifications and the push API
78+
'--disable-notifications',
79+
// disable the prompt when a POST request causes page navigation
80+
'--disable-prompt-on-repost',
81+
// disable syncing browser data with google accounts
82+
'--disable-sync',
83+
// disable site-isolation to make network requests easier to intercept
84+
'--disable-site-isolation-trials',
85+
// disable the first run tasks, whether or not it's actually the first run
86+
'--no-first-run',
87+
// disable the sandbox for all process types that are normally sandboxed
88+
'--no-sandbox',
89+
// specify a consistent encryption backend across platforms
90+
'--password-store=basic',
91+
// use a mock keychain on Mac to prevent blocking permissions dialogs
92+
'--use-mock-keychain',
93+
// enable remote debugging on the first available port
94+
'--remote-debugging-port=0',
95+
// sets navigator.webdriver to false
96+
'--disable-blink-features=AutomationControlled',
97+
// disable UA-CH feature
98+
`--disable-features=UserAgentClientHint`,
99+
],
100+
101+
// discovery request headers
102+
REQUEST_HEADERS: {
103+
// `HeadlessChrome` is added to sec-ch-ua, `--disable-features=UserAgentClientHint` doesn't seem to work
104+
'sec-ch-ua':'"Chromium";v="129", "Not=A?Brand";v="8"',
105+
'sec-ch-ua-mobile': '"?0"',
106+
'sec-ch-ua-platform': '"Windows"'
107+
},
108+
49109
// user agents
50110
CHROME_USER_AGENT: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.107 Safari/537.3',
51111
FIREFOX_USER_AGENT: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:112.0) Gecko/20100101 Firefox/112.0',

src/lib/processSnapshot.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
8080
updateLogContext({ task: 'discovery' });
8181
ctx.log.debug(`Processing snapshot ${snapshot.name} ${snapshot.url}`);
8282

83-
let launchOptions: Record<string, any> = { headless: true }
83+
let launchOptions: Record<string, any> = {
84+
headless: true,
85+
args: constants.LAUNCH_ARGS
86+
}
8487
let contextOptions: Record<string, any> = {
8588
javaScriptEnabled: ctx.config.cliEnableJavaScript,
8689
userAgent: constants.CHROME_USER_AGENT,
@@ -92,8 +95,8 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
9295
}
9396
const context = await ctx.browser.newContext(contextOptions);
9497
ctx.log.debug(`Browser context created with options ${JSON.stringify(contextOptions)}`);
95-
// Setting the cookies in playwright context
96-
if (snapshot.dom.cookies) {
98+
// Setting cookies in playwright context
99+
if (!ctx.env.SMARTUI_DO_NOT_USE_CAPTURED_COOKIES && snapshot.dom.cookies) {
97100
const domainName = new URL(snapshot.url).hostname;
98101
ctx.log.debug(`Setting cookies for domain: ${domainName}`);
99102

@@ -135,7 +138,13 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
135138
await page.route('**/*', async (route, request) => {
136139
const requestUrl = request.url()
137140
const requestHostname = new URL(requestUrl).hostname;
138-
let requestOptions: Record<string, any> = { timeout: REQUEST_TIMEOUT }
141+
let requestOptions: Record<string, any> = {
142+
timeout: REQUEST_TIMEOUT,
143+
headers: {
144+
...await request.allHeaders(),
145+
...constants.REQUEST_HEADERS
146+
}
147+
}
139148

140149
try {
141150
// abort audio/video media requests
@@ -149,10 +158,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
149158
if (ctx.config.basicAuthorization) {
150159
ctx.log.debug(`Adding basic authorization to the headers for root url`);
151160
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
152-
requestOptions.headers = {
153-
...request.headers(),
154-
Authorization: `Basic ${token}`
155-
};
161+
requestOptions.headers.Authorization = `Basic ${token}`;
156162
}
157163

158164
// get response

0 commit comments

Comments
 (0)