feature: add stealth mode, launch args, and init scripts to BrowserOptions
Add anti-bot-detection evasion support to reduce blocking by sites like archive.ph. Stealth mode is enabled by default for all browsers and applies common evasions: navigator.webdriver override, plugin/mimeType spoofing, window.chrome stub, and outerWidth/outerHeight fixes. For Chromium, --disable-blink-features=AutomationControlled is also added. New BrowserOptions fields: - Stealth *bool: toggle stealth presets (default true) - LaunchArgs []string: custom browser launch arguments - InitScripts []string: JavaScript injected before page scripts Closes #56 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
72
stealth_test.go
Normal file
72
stealth_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package extractor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMergeOptions_StealthDefault(t *testing.T) {
|
||||
base := BrowserOptions{Stealth: Bool(true)}
|
||||
got := mergeOptions(base, nil)
|
||||
if got.Stealth == nil || !*got.Stealth {
|
||||
t.Fatal("expected stealth to default to true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_StealthOverrideFalse(t *testing.T) {
|
||||
base := BrowserOptions{Stealth: Bool(true)}
|
||||
got := mergeOptions(base, []BrowserOptions{{Stealth: Bool(false)}})
|
||||
if got.Stealth == nil || *got.Stealth {
|
||||
t.Fatal("expected stealth to be overridden to false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_LaunchArgsAppend(t *testing.T) {
|
||||
base := BrowserOptions{LaunchArgs: []string{"--arg1"}}
|
||||
got := mergeOptions(base, []BrowserOptions{{LaunchArgs: []string{"--arg2", "--arg3"}}})
|
||||
if len(got.LaunchArgs) != 3 {
|
||||
t.Fatalf("expected 3 launch args, got %d", len(got.LaunchArgs))
|
||||
}
|
||||
if got.LaunchArgs[0] != "--arg1" || got.LaunchArgs[1] != "--arg2" || got.LaunchArgs[2] != "--arg3" {
|
||||
t.Fatalf("unexpected launch args: %v", got.LaunchArgs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_InitScriptsAppend(t *testing.T) {
|
||||
base := BrowserOptions{InitScripts: []string{"script1"}}
|
||||
got := mergeOptions(base, []BrowserOptions{{InitScripts: []string{"script2"}}})
|
||||
if len(got.InitScripts) != 2 {
|
||||
t.Fatalf("expected 2 init scripts, got %d", len(got.InitScripts))
|
||||
}
|
||||
if got.InitScripts[0] != "script1" || got.InitScripts[1] != "script2" {
|
||||
t.Fatalf("unexpected init scripts: %v", got.InitScripts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOptions_StealthNilDoesNotOverride(t *testing.T) {
|
||||
base := BrowserOptions{Stealth: Bool(true)}
|
||||
got := mergeOptions(base, []BrowserOptions{{Stealth: nil}})
|
||||
if got.Stealth == nil || !*got.Stealth {
|
||||
t.Fatal("expected stealth to remain true when override is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStealthChromiumArgs(t *testing.T) {
|
||||
if len(stealthChromiumArgs) == 0 {
|
||||
t.Fatal("expected at least one chromium stealth arg")
|
||||
}
|
||||
found := false
|
||||
for _, arg := range stealthChromiumArgs {
|
||||
if arg == "--disable-blink-features=AutomationControlled" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("expected --disable-blink-features=AutomationControlled in stealth chromium args")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStealthInitScripts(t *testing.T) {
|
||||
if len(stealthInitScripts) == 0 {
|
||||
t.Fatal("expected at least one stealth init script")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user