Set default viewport for NewBrowser and align User-Agent with browser engine #70

Closed
opened 2026-02-24 01:13:30 +00:00 by Claude · 2 comments
Collaborator

Parent Epic: #68

Problem

Missing viewport in NewBrowser

NewInteractiveBrowser sets ViewportSize: &playwright.Size{Width: 1280, Height: 720}, but NewBrowser sets no viewport at all. A missing viewport is a strong headless signal — real browsers always have a viewport. Many anti-bot systems check for this.

User-Agent / browser engine mismatch

The codebase defines defaultFirefoxUserAgent and uses it as a fallback, but there's no equivalent Chromium UA constant. When a caller uses BrowserChromium without explicitly setting a UA, the browser may end up with:

  • The Playwright default UA (contains HeadlessChrome)
  • Or the Firefox UA constant applied to a Chromium instance

Either case is a detectable mismatch.

Proposed Solution

Viewport

Add a default viewport to NewBrowser when none is provided:

if opt.ViewportSize == nil {
    opt.ViewportSize = &playwright.Size{Width: 1920, Height: 1080}
}

Use 1920x1080 as the default (most common desktop resolution globally).

User-Agent alignment

  1. Add a defaultChromiumUserAgent constant alongside the existing Firefox one
  2. In NewBrowser / browser init, auto-select the matching UA based on opt.Browser when the caller hasn't explicitly set opt.UserAgent
  3. Ensure the stealth script that strips HeadlessChrome from the UA only runs on Chromium

Files to Modify

  • playwright.go — add default viewport logic, add Chromium UA constant
  • browser_init.go — auto-select UA based on browser engine
  • stealth_test.go — test viewport defaults and UA selection

References

  • #68 — parent epic
**Parent Epic:** #68 ## Problem ### Missing viewport in `NewBrowser` `NewInteractiveBrowser` sets `ViewportSize: &playwright.Size{Width: 1280, Height: 720}`, but `NewBrowser` sets no viewport at all. A missing viewport is a strong headless signal — real browsers always have a viewport. Many anti-bot systems check for this. ### User-Agent / browser engine mismatch The codebase defines `defaultFirefoxUserAgent` and uses it as a fallback, but there's no equivalent Chromium UA constant. When a caller uses `BrowserChromium` without explicitly setting a UA, the browser may end up with: - The Playwright default UA (contains `HeadlessChrome`) - Or the Firefox UA constant applied to a Chromium instance Either case is a detectable mismatch. ## Proposed Solution ### Viewport Add a default viewport to `NewBrowser` when none is provided: ```go if opt.ViewportSize == nil { opt.ViewportSize = &playwright.Size{Width: 1920, Height: 1080} } ``` Use 1920x1080 as the default (most common desktop resolution globally). ### User-Agent alignment 1. Add a `defaultChromiumUserAgent` constant alongside the existing Firefox one 2. In `NewBrowser` / browser init, auto-select the matching UA based on `opt.Browser` when the caller hasn't explicitly set `opt.UserAgent` 3. Ensure the stealth script that strips `HeadlessChrome` from the UA only runs on Chromium ## Files to Modify - `playwright.go` — add default viewport logic, add Chromium UA constant - `browser_init.go` — auto-select UA based on browser engine - `stealth_test.go` — test viewport defaults and UA selection ## References - #68 — parent epic
Claude added the bugpriority/hightype/task labels 2026-02-24 01:13:49 +00:00
Author
Collaborator

Starting implementation. Plan:

1. Per-engine UA constants (playwright.go)

  • Add DefaultFirefoxUserAgent (existing value) and DefaultChromiumUserAgent (Chrome/131 Windows 10 UA)
  • Keep DefaultUserAgent = DefaultFirefoxUserAgent as backward-compatible alias
  • Remove hardcoded UserAgent: DefaultUserAgent from NewBrowser base defaults so initBrowser can auto-select

2. Default viewport (playwright.go)

  • Add Dimensions: Size{Width: 1920, Height: 1080} to NewBrowser base defaults (matching issue recommendation)

3. UA auto-selection (browser_init.go)

  • After browser type switch, if opt.UserAgent == "", select DefaultChromiumUserAgent or DefaultFirefoxUserAgent based on opt.Browser

4. Interactive browser alignment (interactive.go)

  • Remove UserAgent: DefaultUserAgent from NewInteractiveBrowser base defaults (same auto-selection)

5. Tests (stealth_test.go)

  • UA constant content validation, backward compat alias, mergeOptions UA/viewport behavior

Branch: fix/70-default-viewport-ua-alignment

Starting implementation. Plan: **1. Per-engine UA constants (`playwright.go`)** - Add `DefaultFirefoxUserAgent` (existing value) and `DefaultChromiumUserAgent` (Chrome/131 Windows 10 UA) - Keep `DefaultUserAgent = DefaultFirefoxUserAgent` as backward-compatible alias - Remove hardcoded `UserAgent: DefaultUserAgent` from `NewBrowser` base defaults so `initBrowser` can auto-select **2. Default viewport (`playwright.go`)** - Add `Dimensions: Size{Width: 1920, Height: 1080}` to `NewBrowser` base defaults (matching issue recommendation) **3. UA auto-selection (`browser_init.go`)** - After browser type switch, if `opt.UserAgent == ""`, select `DefaultChromiumUserAgent` or `DefaultFirefoxUserAgent` based on `opt.Browser` **4. Interactive browser alignment (`interactive.go`)** - Remove `UserAgent: DefaultUserAgent` from `NewInteractiveBrowser` base defaults (same auto-selection) **5. Tests (`stealth_test.go`)** - UA constant content validation, backward compat alias, mergeOptions UA/viewport behavior Branch: `fix/70-default-viewport-ua-alignment`
Author
Collaborator

Implementation complete. PR: #73

What was done:

  • Added DefaultFirefoxUserAgent and DefaultChromiumUserAgent constants; DefaultUserAgent kept as backward-compatible alias
  • initBrowser now auto-selects the matching UA based on opt.Browser when none is explicitly set — fixes Firefox UA being applied to Chromium instances
  • Added 1920x1080 default viewport to NewBrowser (previously nil — a strong headless signal)
  • NewInteractiveBrowser also gains engine-aware UA selection (keeps its 1280x720 viewport)
  • 8 new tests: UA constant content validation, backward-compat alias, mergeOptions viewport/UA behavior
  • All builds, tests, and vet pass cleanly
Implementation complete. PR: #73 **What was done:** - Added `DefaultFirefoxUserAgent` and `DefaultChromiumUserAgent` constants; `DefaultUserAgent` kept as backward-compatible alias - `initBrowser` now auto-selects the matching UA based on `opt.Browser` when none is explicitly set — fixes Firefox UA being applied to Chromium instances - Added 1920x1080 default viewport to `NewBrowser` (previously nil — a strong headless signal) - `NewInteractiveBrowser` also gains engine-aware UA selection (keeps its 1280x720 viewport) - 8 new tests: UA constant content validation, backward-compat alias, mergeOptions viewport/UA behavior - All builds, tests, and vet pass cleanly
steve closed this issue 2026-02-24 01:29:37 +00:00
Sign in to join this conversation.