browser

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package browser holds the persistent browser session: a long-lived chromedp browser with one or more tabs. The current tab holds the cached accessibility tree so find/filter is free (no new CDP round-trip). A mutex serializes all operations; each public method is atomic.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSnapshot = errors.New("no page snapshot yet on the current tab; call navigate first")

ErrNoSnapshot is returned when the current tab has no cached page tree.

Functions

func ValidateURL

func ValidateURL(raw string, allowInsecure bool) (string, error)

ValidateURL checks a navigation URL is safe. By default only http/https are allowed; file/javascript/data/about/blob require allowInsecure (opt-in) so an agent can't be steered to read local files or execute javascript: URIs. Relative URLs (no scheme) are rejected.

Types

type Config

type Config struct {
	Headless    bool
	Timeout     time.Duration // >0 bounds the first tab (debug CLI); 0 = long-lived (MCP server)
	UserDataDir string        // persistent profile dir; "" = a throwaway temp profile (the MCP server defaults this to <os config dir>/agent-browser for persistence unless --no-persist)
	Proxy       string        // proxy server URL (e.g. http://user:pass@host:port); "" = none
	UserAgent   string        // override the User-Agent; "" = Chrome default
	ViewportW   int           // window width; 0 = 1366
	ViewportH   int           // window height; 0 = 768
	Stealth     bool          // apply anti-detection flags + init script + jittered mouse (default true)
}

Config configures a Session.

type Session

type Session struct {

	// AllowInsecureSchemes opts in to file/javascript/data/about/blob URLs.
	AllowInsecureSchemes bool
	// AllowEval controls the eval tool (arbitrary page JS). On by default;
	// the operator can disable it with the --no-eval flag.
	AllowEval bool
	// contains filtered or unexported fields
}

Session is a persistent Chrome browser with a current tab and optional additional tabs.

func New

func New(cfg Config) (*Session, error)

New launches Chrome and returns a Session with one initial tab.

func (*Session) BuildTree

func (s *Session) BuildTree() error

BuildTree pulls the accessibility tree for the current tab and caches it.

func (*Session) ClickAndSee

func (s *Session) ClickAndSee(ref string, settle time.Duration) (*snapshot.Delta, *snapshot.Tree, error)

ClickAndSee clicks a ref (real mouse) and returns the delta + new tree.

func (*Session) Close

func (s *Session) Close()

Close shuts down the browser.

func (*Session) CloseTab

func (s *Session) CloseTab(idOrLabel string) error

CloseTab closes the tab with the given id (or label). The last tab cannot be closed (the session must always have one).

func (*Session) Eval

func (s *Session) Eval(script string) (string, error)

Eval runs arbitrary JS in the current tab and returns the raw JSON result. Gated by AllowEval (on by default; operator can disable with --no-eval).

func (*Session) FillAndSee

func (s *Session) FillAndSee(ref, value string, settle time.Duration) (*snapshot.Delta, *snapshot.Tree, error)

FillAndSee sets an input value (dispatches input+change) and returns delta.

func (*Session) FillText

func (s *Session) FillText() error

FillText fetches the current tab's visible text (walking same-origin iframes) and attaches it to the cached tree so a full-level Render includes it. Use before Render(LevelFull). Returns ErrNoSnapshot if there is no cached tree.

func (*Session) HoverAndSee

func (s *Session) HoverAndSee(ref string, settle time.Duration) (*snapshot.Delta, *snapshot.Tree, error)

HoverAndSee hovers an element by ref and returns the delta (e.g. a hover menu appearing as Added elements).

func (*Session) Navigate

func (s *Session) Navigate(raw string) error

Navigate opens a URL on the current tab and waits for the body. Invalidates the cached tree.

func (*Session) NavigateAndSee

func (s *Session) NavigateAndSee(raw string) (*snapshot.Tree, error)

NavigateAndSee navigates the current tab and returns its new tree. Atomic.

func (*Session) NewTab

func (s *Session) NewTab(url string) (*snapshot.Tree, error)

NewTab opens a new tab (navigating to url if non-empty), makes it current, and returns its tree (nil if url is empty).

func (*Session) PressKeyAndSee

func (s *Session) PressKeyAndSee(key, modifiers string, settle time.Duration) (*snapshot.Delta, *snapshot.Tree, error)

PressKeyAndSee dispatches a real keyDown + keyUp (CDP Input.dispatchKeyEvent) on the focused element and returns the delta. Real key events fire native default actions (Enter submits, Escape closes, Tab moves focus, a char inserts) - synthetic JS KeyboardEvents do not.

func (*Session) Read

func (s *Session) Read(ref string, offset int) (string, error)

Read returns url + title, plus a ref's text (if ref given) or body text. For the body (no ref), offset skips that many chars (pagination).

func (*Session) Screenshot

func (s *Session) Screenshot() ([]byte, error)

Screenshot captures the current tab's viewport as a PNG.

func (*Session) ScrollAndSee

func (s *Session) ScrollAndSee(dx, dy int, settle time.Duration) (*snapshot.Delta, *snapshot.Tree, error)

ScrollAndSee scrolls by dx/dy CSS pixels and returns the delta.

func (*Session) SelectAndSee

func (s *Session) SelectAndSee(ref, value string, settle time.Duration) (*snapshot.Delta, *snapshot.Tree, error)

SelectAndSee sets a <select> value and returns the delta.

func (*Session) SetTabLabel

func (s *Session) SetTabLabel(label string) error

SetTabLabel sets a label on the current tab (optional, for memorable names).

func (*Session) SwitchTab

func (s *Session) SwitchTab(idOrLabel string) error

SwitchTab makes the tab with the given id (or label) current.

func (*Session) Tabs

func (s *Session) Tabs() []TabInfo

Tabs lists all tabs.

func (*Session) Tree

func (s *Session) Tree() *snapshot.Tree

Tree returns the current tab's cached page tree (nil if none).

func (*Session) Upload

func (s *Session) Upload(ref string, paths []string) error

Upload sets files on a file input. If ref is non-empty it targets that element; otherwise it auto-finds the first <input type=file> on the page (file inputs are usually absent from the a11y tree, so ref-less auto-find is the common path).

func (*Session) Wait

func (s *Session) Wait(d time.Duration, text string) error

Wait blocks up to d; if text is set, returns early once it appears in body.

type TabInfo

type TabInfo struct {
	ID      string `json:"id"`
	Label   string `json:"label"`
	URL     string `json:"url"`
	Title   string `json:"title"`
	Current bool   `json:"current"`
}

TabInfo is a tab's summary for the tabs tool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL