fix: set default viewport for NewBrowser and align User-Agent with engine
All checks were successful
CI / vet (pull_request) Successful in 1m6s
CI / build (pull_request) Successful in 1m11s
CI / test (pull_request) Successful in 1m11s

NewBrowser previously had no viewport (strong headless signal) and used a
Firefox User-Agent unconditionally, even for Chromium instances (detectable
mismatch).

Add per-engine UA constants (DefaultFirefoxUserAgent, DefaultChromiumUserAgent)
and auto-select the matching UA in initBrowser when the caller hasn't set one
explicitly. Keep DefaultUserAgent as a backward-compatible alias.

Add 1920x1080 default viewport to NewBrowser (most common desktop resolution).
NewInteractiveBrowser keeps its existing 1280x720 default but also gains
engine-aware UA selection.

Closes #70

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 01:28:09 +00:00
parent ff1d6c491a
commit 6647e4f63d
4 changed files with 94 additions and 11 deletions

View File

@@ -36,8 +36,14 @@ const (
BrowserWebKit BrowserSelection = "webkit"
)
// DefaultUserAgent is the user-agent string used by all browser instances.
const DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0"
// DefaultFirefoxUserAgent is the user-agent string used for Firefox browser instances.
const DefaultFirefoxUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0"
// DefaultChromiumUserAgent is the user-agent string used for Chromium browser instances.
const DefaultChromiumUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
// DefaultUserAgent is an alias for DefaultFirefoxUserAgent, retained for backward compatibility.
const DefaultUserAgent = DefaultFirefoxUserAgent
// Bool returns a pointer to the given bool value.
func Bool(v bool) *bool { return &v }
@@ -47,7 +53,7 @@ type Size struct {
Height int
}
type BrowserOptions struct {
UserAgent string // If empty, defaults to DefaultUserAgent
UserAgent string // If empty, auto-selected based on Browser engine
Browser BrowserSelection // If unset defaults to Firefox.
Timeout *time.Duration // If unset defaults to 30 seconds timeout. If set to 0, no timeout
@@ -145,10 +151,13 @@ func playwrightCookieToCookie(cookie playwright.Cookie) Cookie {
func NewBrowser(ctx context.Context, opts ...BrowserOptions) (Browser, error) {
var thirtySeconds = 30 * time.Second
opt := mergeOptions(BrowserOptions{
UserAgent: DefaultUserAgent,
Browser: BrowserFirefox,
Timeout: &thirtySeconds,
Stealth: Bool(true),
Browser: BrowserFirefox,
Timeout: &thirtySeconds,
Stealth: Bool(true),
Dimensions: Size{
Width: 1920,
Height: 1080,
},
}, opts)
if err := ctx.Err(); err != nil {