playwright-stealth —— Configure Playwright's Automated Browser Environment
Make Playwright automation session environments look more like real users, reducing exposure of machine characteristics. Extracted from caijiqi-kaifa/electron/collector/stealth.ts. Requires Playwright runtime (browser-side injection depends on
pageobject). Includes a minimal built-in sample of UA/resolution; for production, pair withbrowser-fingerprint-pool(88+ UA / 87 resolutions).
Compliance Notes
This skill is solely intended to make automated browsers appear closer to real user environments, which is a general browser environment configuration capability. Always comply with the target site's terms of service and applicable laws and regulations. Only use this for compliant automation and testing scenarios.
Use Cases
- When using Playwright for web automation, scraping, or regression testing and you want a more realistic, stable environment
- When you need to randomize UA, viewport, language, timezone and keep those features self-consistent (e.g., resolution matches viewport)
- When you need to fill in Canvas, WebGL, AudioContext, Plugins, Permissions and other browser features to avoid exposing empty or missing characteristics
Usage
import { getRandomBrowserConfig, STEALTH_ARGS, IGNORE_DEFAULT_ARGS,
injectStealthScripts, createStealthContext }
from './scripts/stealth.mjs';
import { chromium } from 'playwright';
const browser = await chromium.launch({ args: STEALTH_ARGS, ignoreDefaultArgs: IGNORE_DEFAULT_ARGS });
// Option 1: Directly get a random self-consistent configuration
const cfg = getRandomBrowserConfig('US');
// => { userAgent, viewport:{width,height}, locale, languages, timezoneId }
// Option 2: Create an injected context (recommended)
const { context, config } = await createStealthContext(browser, 'DE');
const page = await context.newPage();
await page.goto('https://example.com');
Core API
getRandomBrowserConfig(site): Returns a random self-consistent configuration (UA, viewport, language, timezone) for the given site region (supports US/UK/DE/FR/IT/ES/JP/CA/AU/IN/MX, defaults to US)STEALTH_ARGS: Chromium launch arguments (removes automation related flags, disables background throttling, etc.)IGNORE_DEFAULT_ARGS: Default arguments to ignore (e.g.,--enable-automation)injectStealthScripts(page): Injects environment simulation scripts into the page (hides webdriver, simulates plugins/Chrome/Canvas/Audio/WebRTC/Permissions/resolution consistency). Must run inside the browsercreateStealthContext(browser, site, proxy?): Creates a browser context with environment simulation already injected
Important Notes
injectStealthScripts/createStealthContextmust be executed within the Playwright browser runtime; purely in Node they cannot be unit tested. You can usegetRandomBrowserConfig/STEALTH_ARGSto verify the pure logic.- The built-in UA / resolution data is only a minimal sample. For a larger sample, replace the data in
RANDOM_UAS/RANDOM_RESOLUTIONSwith data frombrowser-fingerprint-pool.