Extract buildContextOptions from the three context sites

launch(), recreateContext(), and recreateContext()'s clean-slate
fallback each built their own BrowserContextOptions from the same
manager fields. Every context-level setting has to be repeated in all
three or it silently fails to survive a rebuild, and the three copies
had already drifted in shape.

Collapse them onto one builder so a new context option is added in one
place. No behavior change: the resulting options are identical.
This commit is contained in:
Topher Hindman 2026-08-09 10:50:00 -07:00
parent 94993f7401
commit c31e29c548
1 changed files with 22 additions and 24 deletions

View File

@ -169,6 +169,7 @@ export class BrowserManager {
private deviceScaleFactor: number = 1; private deviceScaleFactor: number = 1;
private currentViewport: { width: number; height: number } = { width: 1280, height: 720 }; private currentViewport: { width: number; height: number } = { width: 1280, height: 720 };
/** Server port — set after server starts, used by cookie-import-browser command */ /** Server port — set after server starts, used by cookie-import-browser command */
public serverPort: number = 0; public serverPort: number = 0;
@ -394,14 +395,7 @@ export class BrowserManager {
void handleChromiumDisconnect(this.browser); void handleChromiumDisconnect(this.browser);
}); });
const contextOptions: BrowserContextOptions = { this.context = await this.browser.newContext(this.buildContextOptions());
viewport: { width: this.currentViewport.width, height: this.currentViewport.height },
deviceScaleFactor: this.deviceScaleFactor,
};
if (this.customUserAgent) {
contextOptions.userAgent = this.customUserAgent;
}
this.context = await this.browser.newContext(contextOptions);
if (Object.keys(this.extraHeaders).length > 0) { if (Object.keys(this.extraHeaders).length > 0) {
await this.context.setExtraHTTPHeaders(this.extraHeaders); await this.context.setExtraHTTPHeaders(this.extraHeaders);
@ -1414,14 +1408,7 @@ export class BrowserManager {
await this.context.close().catch(() => {}); await this.context.close().catch(() => {});
// 3. Create new context with updated settings // 3. Create new context with updated settings
const contextOptions: BrowserContextOptions = { this.context = await this.browser.newContext(this.buildContextOptions());
viewport: { width: this.currentViewport.width, height: this.currentViewport.height },
deviceScaleFactor: this.deviceScaleFactor,
};
if (this.customUserAgent) {
contextOptions.userAgent = this.customUserAgent;
}
this.context = await this.browser.newContext(contextOptions);
// Re-apply stealth: newContext() is a fresh context with no init scripts, // Re-apply stealth: newContext() is a fresh context with no init scripts,
// so a useragent / viewport --scale rebuild would otherwise drop the // so a useragent / viewport --scale rebuild would otherwise drop the
@ -1446,14 +1433,7 @@ export class BrowserManager {
this.tabSessions.clear(); this.tabSessions.clear();
if (this.context) await this.context.close().catch(() => {}); if (this.context) await this.context.close().catch(() => {});
const contextOptions: BrowserContextOptions = { this.context = await this.browser!.newContext(this.buildContextOptions());
viewport: { width: this.currentViewport.width, height: this.currentViewport.height },
deviceScaleFactor: this.deviceScaleFactor,
};
if (this.customUserAgent) {
contextOptions.userAgent = this.customUserAgent;
}
this.context = await this.browser!.newContext(contextOptions);
// Stealth applies to the fallback blank context too. // Stealth applies to the fallback blank context too.
const { applyStealth } = await import('./stealth'); const { applyStealth } = await import('./stealth');
await applyStealth(this.context); await applyStealth(this.context);
@ -1466,6 +1446,24 @@ export class BrowserManager {
} }
} }
/**
* Context options every context in this manager is built from — the initial
* launch, the recreateContext() rebuild, and its clean-slate fallback. These
* are the settings that only apply at context construction, so each one has to
* survive a rebuild; keeping the single builder here is what stops the three
* call sites from drifting apart.
*/
private buildContextOptions(): BrowserContextOptions {
const options: BrowserContextOptions = {
viewport: { width: this.currentViewport.width, height: this.currentViewport.height },
deviceScaleFactor: this.deviceScaleFactor,
};
if (this.customUserAgent) {
options.userAgent = this.customUserAgent;
}
return options;
}
/** /**
* Change deviceScaleFactor + viewport size atomically. * Change deviceScaleFactor + viewport size atomically.
* *