2026-02-23 19:57:19 +01:00
|
|
|
/**
|
|
|
|
|
* Playwright launch wrapper for cloakbrowser.
|
|
|
|
|
* Mirrors Python cloakbrowser/browser.py.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type { Browser, BrowserContext } from "playwright-core";
|
2026-03-04 12:00:49 +03:00
|
|
|
import type { LaunchOptions, LaunchContextOptions, LaunchPersistentContextOptions } from "./types.js";
|
2026-02-27 03:56:54 +01:00
|
|
|
import { DEFAULT_VIEWPORT, getDefaultStealthArgs } from "./config.js";
|
2026-02-23 19:57:19 +01:00
|
|
|
import { ensureBinary } from "./download.js";
|
2026-02-24 19:00:12 +01:00
|
|
|
import { parseProxyUrl } from "./proxy.js";
|
2026-02-23 19:57:19 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Launch stealth Chromium browser via Playwright.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* import { launch } from 'cloakbrowser';
|
|
|
|
|
* const browser = await launch();
|
|
|
|
|
* const page = await browser.newPage();
|
|
|
|
|
* await page.goto('https://bot.incolumitas.com');
|
|
|
|
|
* console.log(await page.title());
|
|
|
|
|
* await browser.close();
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export async function launch(options: LaunchOptions = {}): Promise<Browser> {
|
|
|
|
|
const { chromium } = await import("playwright-core");
|
|
|
|
|
|
|
|
|
|
const binaryPath = process.env.CLOAKBROWSER_BINARY_PATH || (await ensureBinary());
|
2026-03-01 01:07:11 +01:00
|
|
|
const resolved = await maybeResolveGeoip(options);
|
|
|
|
|
const args = buildArgs({ ...options, ...resolved });
|
2026-02-23 19:57:19 +01:00
|
|
|
|
|
|
|
|
const browser = await chromium.launch({
|
|
|
|
|
executablePath: binaryPath,
|
|
|
|
|
headless: options.headless ?? true,
|
|
|
|
|
args,
|
|
|
|
|
ignoreDefaultArgs: ["--enable-automation"],
|
2026-03-04 20:11:30 +01:00
|
|
|
...(options.proxy
|
|
|
|
|
? { proxy: typeof options.proxy === "string" ? parseProxyUrl(options.proxy) : options.proxy }
|
|
|
|
|
: {}),
|
2026-02-23 19:57:19 +01:00
|
|
|
...options.launchOptions,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Launch stealth browser and return a BrowserContext with common options pre-set.
|
|
|
|
|
* Closing the context also closes the browser.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* import { launchContext } from 'cloakbrowser';
|
|
|
|
|
* const context = await launchContext({
|
|
|
|
|
* userAgent: 'Mozilla/5.0...',
|
|
|
|
|
* viewport: { width: 1920, height: 1080 },
|
|
|
|
|
* });
|
|
|
|
|
* const page = await context.newPage();
|
|
|
|
|
* await page.goto('https://example.com');
|
|
|
|
|
* await context.close(); // also closes browser
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export async function launchContext(
|
|
|
|
|
options: LaunchContextOptions = {}
|
|
|
|
|
): Promise<BrowserContext> {
|
2026-03-01 01:07:11 +01:00
|
|
|
// Resolve geoip BEFORE launch() to avoid double-resolution
|
|
|
|
|
const resolved = await maybeResolveGeoip(options);
|
2026-03-04 20:11:30 +01:00
|
|
|
// Skip --fingerprint-timezone binary flag: it only applies to the default
|
|
|
|
|
// context and interferes with Playwright's timezoneId on new contexts.
|
|
|
|
|
// Timezone is set via browser.newContext(timezoneId: ...) below instead.
|
|
|
|
|
const browser = await launch({ ...options, ...resolved, geoip: false, timezone: undefined });
|
2026-02-23 19:57:19 +01:00
|
|
|
|
|
|
|
|
let context: BrowserContext;
|
|
|
|
|
try {
|
|
|
|
|
context = await browser.newContext({
|
|
|
|
|
...(options.userAgent ? { userAgent: options.userAgent } : {}),
|
2026-02-27 03:56:54 +01:00
|
|
|
viewport: options.viewport ?? DEFAULT_VIEWPORT,
|
2026-03-01 01:07:11 +01:00
|
|
|
...(resolved.locale ? { locale: resolved.locale } : {}),
|
|
|
|
|
...(resolved.timezone ? { timezoneId: resolved.timezone } : {}),
|
2026-03-01 02:21:53 +01:00
|
|
|
...(options.colorScheme ? { colorScheme: options.colorScheme } : {}),
|
2026-02-23 19:57:19 +01:00
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
await browser.close();
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Patch close() to also close the browser
|
|
|
|
|
const origClose = context.close.bind(context);
|
|
|
|
|
context.close = async () => {
|
|
|
|
|
await origClose();
|
|
|
|
|
await browser.close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 12:00:49 +03:00
|
|
|
/**
|
|
|
|
|
* Launch stealth browser with a persistent user profile (non-incognito).
|
|
|
|
|
* Uses Playwright's chromium.launchPersistentContext() under the hood.
|
|
|
|
|
*
|
|
|
|
|
* This avoids incognito detection by services like BrowserScan (-10% penalty)
|
|
|
|
|
* and enables session persistence (cookies, localStorage) across launches.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* import { launchPersistentContext } from 'cloakbrowser';
|
|
|
|
|
* const context = await launchPersistentContext({
|
|
|
|
|
* userDataDir: './chrome-profile',
|
|
|
|
|
* headless: false,
|
|
|
|
|
* proxy: 'http://user:pass@host:port',
|
|
|
|
|
* geoip: true,
|
|
|
|
|
* });
|
|
|
|
|
* const page = context.pages()[0] || await context.newPage();
|
|
|
|
|
* await page.goto('https://example.com');
|
|
|
|
|
* await context.close();
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export async function launchPersistentContext(
|
|
|
|
|
options: LaunchPersistentContextOptions
|
|
|
|
|
): Promise<BrowserContext> {
|
|
|
|
|
const { chromium } = await import("playwright-core");
|
|
|
|
|
|
|
|
|
|
const binaryPath = process.env.CLOAKBROWSER_BINARY_PATH || (await ensureBinary());
|
|
|
|
|
const resolved = await maybeResolveGeoip(options);
|
|
|
|
|
const args = buildArgs({ ...options, ...resolved });
|
|
|
|
|
|
|
|
|
|
const context = await chromium.launchPersistentContext(options.userDataDir, {
|
|
|
|
|
executablePath: binaryPath,
|
|
|
|
|
headless: options.headless ?? true,
|
|
|
|
|
args,
|
|
|
|
|
ignoreDefaultArgs: ["--enable-automation"],
|
2026-03-04 20:11:30 +01:00
|
|
|
...(options.proxy
|
|
|
|
|
? { proxy: typeof options.proxy === "string" ? parseProxyUrl(options.proxy) : options.proxy }
|
|
|
|
|
: {}),
|
2026-03-04 12:00:49 +03:00
|
|
|
...(options.userAgent ? { userAgent: options.userAgent } : {}),
|
|
|
|
|
viewport: options.viewport ?? DEFAULT_VIEWPORT,
|
|
|
|
|
...(resolved.locale ? { locale: resolved.locale } : {}),
|
|
|
|
|
...(resolved.timezone ? { timezoneId: resolved.timezone } : {}),
|
|
|
|
|
...(options.colorScheme ? { colorScheme: options.colorScheme } : {}),
|
|
|
|
|
...options.launchOptions,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 19:57:19 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Internal
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
2026-03-01 01:07:11 +01:00
|
|
|
async function maybeResolveGeoip(
|
|
|
|
|
options: LaunchOptions
|
|
|
|
|
): Promise<{ timezone?: string; locale?: string }> {
|
|
|
|
|
if (!options.geoip || !options.proxy) return { timezone: options.timezone, locale: options.locale };
|
|
|
|
|
if (options.timezone && options.locale) return { timezone: options.timezone, locale: options.locale };
|
|
|
|
|
|
|
|
|
|
const { resolveProxyGeo } = await import("./geoip.js");
|
2026-03-04 20:11:30 +01:00
|
|
|
const proxyUrl = typeof options.proxy === "string" ? options.proxy : options.proxy.server;
|
|
|
|
|
if (!proxyUrl) return { timezone: options.timezone, locale: options.locale };
|
|
|
|
|
const { timezone: geoTz, locale: geoLocale } = await resolveProxyGeo(proxyUrl);
|
2026-03-01 01:07:11 +01:00
|
|
|
return {
|
|
|
|
|
timezone: options.timezone ?? geoTz ?? undefined,
|
|
|
|
|
locale: options.locale ?? geoLocale ?? undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @internal Exposed for unit tests only. */
|
|
|
|
|
export function _buildArgsForTest(options: LaunchOptions): string[] {
|
|
|
|
|
return buildArgs(options);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 19:57:19 +01:00
|
|
|
function buildArgs(options: LaunchOptions): string[] {
|
|
|
|
|
const args: string[] = [];
|
|
|
|
|
if (options.stealthArgs !== false) {
|
|
|
|
|
args.push(...getDefaultStealthArgs());
|
|
|
|
|
}
|
|
|
|
|
if (options.args) {
|
|
|
|
|
args.push(...options.args);
|
|
|
|
|
}
|
2026-03-01 01:07:11 +01:00
|
|
|
// Timezone/locale flags — always inject when set
|
|
|
|
|
if (options.timezone) {
|
2026-03-02 18:20:57 +01:00
|
|
|
args.push(`--fingerprint-timezone=${options.timezone}`);
|
2026-03-01 01:07:11 +01:00
|
|
|
}
|
|
|
|
|
if (options.locale) {
|
|
|
|
|
args.push(`--lang=${options.locale}`);
|
|
|
|
|
}
|
2026-02-23 19:57:19 +01:00
|
|
|
return args;
|
|
|
|
|
}
|