Documentation
¶
Overview ¶
Package cfbrowse drives Chrome over CDP without ever sending Runtime.enable.
Runtime.enable is the command anti-bot vendors probe for: enabling it changes observable behaviour inside the page, and no amount of fingerprint patching hides it. Everything here evaluates JavaScript through an isolated world created with Page.createIsolatedWorld instead, which needs no Runtime domain.
Index ¶
- Constants
- type Browser
- func (b *Browser) Close() error
- func (b *Browser) Cookies() ([]Cookie, error)
- func (b *Browser) Eval(js string) (json.RawMessage, error)
- func (b *Browser) EvalString(js string) (string, error)
- func (b *Browser) HasClearance() bool
- func (b *Browser) Navigate(url string) error
- func (b *Browser) SentMethods() []string
- func (b *Browser) Solve(limit time.Duration) (string, error)
- func (b *Browser) SolveProgress(limit time.Duration, onStep func(phase string, attempt int)) (string, error)
- func (b *Browser) UserAgent() string
- func (b *Browser) WaitReady(limit time.Duration) (string, error)
- type Cookie
- type Options
Constants ¶
const ( // StepWaiting: the page is not readable yet, or the widget has not rendered. // It renders a beat after the interstitial, so this is the normal opening. StepWaiting = "waiting" // StepWidget: the widget was located, but this pass is not clicking it — // either the attempt budget is spent or the cooldown has not elapsed. StepWidget = "widget" // StepClicking: a click cycle is being dispatched. attempt is 1-based. StepClicking = "clicking" // StepCleared: the challenge is gone. Solve returns right after this. StepCleared = "cleared" )
Solve's phases, as reported to SolveProgress's callback.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Browser ¶
type Browser struct {
// contains filtered or unexported fields
}
Browser is one Chrome process and the single page target attached to it. It is safe for concurrent use; there is no tab management.
func Launch ¶
Launch starts Chrome and attaches to its page target.
With no Options.UserAgent it starts Chrome twice: once to ask what it is, then for real with a matching --user-agent. See resolveUA for why the extra second is not optional.
func (*Browser) Close ¶
Close shuts Chrome down through Browser.close so the profile keeps the cookies earned this run, then kills the process if it does not exit.
func (*Browser) Cookies ¶
Cookies uses Storage.getCookies, a browser-level command, so the Network domain never has to be enabled.
func (*Browser) Eval ¶
func (b *Browser) Eval(js string) (json.RawMessage, error)
Eval runs js and returns the result as JSON.
The isolated world is recreated per call: it is cheap, and it is invalidated by every navigation, so caching the context id only buys stale-handle bugs. Isolated worlds see the DOM but not the page's own JS globals — fine for scraping, not for reaching into page state.
func (*Browser) EvalString ¶
EvalString is Eval for expressions that return a string. A result that is not a JSON string is returned as its raw JSON rather than as an error.
func (*Browser) HasClearance ¶
HasClearance reports whether a cf_clearance cookie exists. It says nothing about whether the site still honours it — clearance expiry is silent, and the only proof is a request that is not challenged.
func (*Browser) Navigate ¶
Navigate returns as soon as Chrome accepts the command, not when the page has loaded. Follow it with WaitReady, or with Solve if a challenge is expected.
func (*Browser) SentMethods ¶
SentMethods returns every CDP method this browser has sent, in order.
func (*Browser) Solve ¶
Solve gets past an interactive challenge, clicking the Turnstile widget with real mouse input if one appears, and returns the page title once through.
A page that was never challenged returns as soon as its title settles, so Solve is safe to use in place of WaitReady.
func (*Browser) SolveProgress ¶
func (b *Browser) SolveProgress(limit time.Duration, onStep func(phase string, attempt int)) (string, error)
SolveProgress is Solve with a progress callback, for callers that put this wait in front of a person. It can take a minute and a half, and "still waiting for the widget to render" and "clicking it for the third time" are different enough that one spinner for all of it reads as a hang.
onStep may be nil. It fires only when the phase actually changes — the loop polls at 1 Hz and would otherwise report waiting ninety times — and it runs on Solve's own goroutine, so keep it cheap and non-blocking: the poll cadence is itself part of what the site scores.
func (*Browser) UserAgent ¶
UserAgent reports the UA the page actually sends. Callers that store a cf_clearance have to store this alongside it; the cookie is scored against it.
type Cookie ¶
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
}
Cookie is the subset of a CDP cookie this package reads: enough to find cf_clearance and tell which host issued it.
type Options ¶
type Options struct {
UserDataDir string // persistent profile; keeps cf_clearance between runs
Headless bool
ExecPath string // defaults to the platform's Chrome, see defaultChrome
UserAgent string // defaults to the browser's own UA, see resolveUA
}
Options configures Launch. The zero value launches headed Chrome with a throwaway profile, which loses any cf_clearance when the process exits.