browser

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package browser drives a Chromium-family browser through the Chrome DevTools Protocol for the agent's browser tool (spec_027_parity §5.2): its own throwaway profile, never the user's, with every request it makes going through a proxy that applies the web rules.

Index

Constants

This section is empty.

Variables

View Source
var ErrBlockedAddress = errors.New("not a public address")

ErrBlockedAddress means the browser tried to reach an address that isn't public.

View Source
var ErrNoBrowser = errors.New("no Chrome, Chromium, Edge or Brave found: install one or set browser.path")

ErrNoBrowser means no Chromium-family browser was found.

Functions

func Find

func Find(path string) (string, error)

Find is the browser to run: path when set, else the first one installed.

func PrivateAddr added in v1.1.3

func PrivateAddr(addr netip.Addr) bool

PrivateAddr reports whether addr is on a private network: RFC 1918, unique local IPv6 (fc00::/7) or carrier-grade NAT (100.64.0.0/10, which VPNs such as Tailscale use), judged by the IPv4 address an IPv6 one carries as PublicAddr does. Loopback and link-local aren't.

func PublicAddr

func PublicAddr(addr netip.Addr) bool

PublicAddr reports whether addr is a routable public address: not loopback, private, link-local (cloud metadata at 169.254.169.254), multicast, unspecified or reserved. An IPv6 address carrying an IPv4 one (mapped, compatible, NAT64 or 6to4) is judged by that, as the Electron app's rules do. The web tools and the browser share it.

Types

type Browser

type Browser struct {
	// contains filtered or unexported fields
}

Browser is a running browser with one page.

func Attach

func Attach(ctx context.Context, wsURL string, opts Options) (*Browser, error)

Attach drives the page at a DevTools WebSocket address, in a browser someone else started: its network isn't filtered (Options.Host and AllowLocal don't apply), but page loads still go through Navigation.

func AttachTransport added in v0.8.4

func AttachTransport(ctx context.Context, t Transport, opts Options) (*Browser, error)

AttachTransport drives the page at the other end of t, a relay to a browser someone else runs (the desktop app's own view, spec_050_agent_browser): as Attach, its network isn't filtered here (the other end filters it), but page loads still go through Navigation.

func Launch

func Launch(ctx context.Context, opts Options) (*Browser, error)

Launch starts a browser with a new, empty profile, or opts.ProfileDir.

func (*Browser) Back

func (b *Browser) Back(ctx context.Context) (Page, error)

Back goes back a page.

func (*Browser) Click

func (b *Browser) Click(ctx context.Context, selector string) error

Click clicks the element's middle, as a mouse would.

func (*Browser) Close

func (b *Browser) Close() error

Close stops the browser it launched (one it attached to isn't its to close) and removes its profile, unless it's kept (ProfileDir).

func (*Browser) Console

func (b *Browser) Console() []ConsoleLine

Console is the console's messages since the last call.

func (*Browser) Eval

func (b *Browser) Eval(ctx context.Context, script string) (string, error)

Eval runs a script the model wrote in the page and returns its value (awaited, as JSON unless a string).

func (*Browser) HTML

func (b *Browser) HTML(ctx context.Context, selector string, max int) (string, bool, error)

HTML is the page's (or the element's) HTML, up to max bytes.

func (*Browser) Navigate

func (b *Browser) Navigate(ctx context.Context, u string) (Page, error)

Navigate loads u (already decided on) and waits for it.

func (*Browser) Page

func (b *Browser) Page(ctx context.Context) (Page, error)

Page is the current page.

func (*Browser) Refused

func (b *Browser) Refused() []string

Refused is what the browser was stopped from reaching since the last call.

func (*Browser) Screenshot

func (b *Browser) Screenshot(ctx context.Context, full bool) ([]byte, error)

Screenshot is a PNG of the viewport, or of the whole page.

func (*Browser) Select

func (b *Browser) Select(ctx context.Context, selector, value string) error

Select chooses the option of a <select> whose value or text is value.

func (*Browser) Text

func (b *Browser) Text(ctx context.Context, selector string, max int) (string, bool, error)

Text is the page's readable text (or the element's), up to max bytes.

func (*Browser) Type

func (b *Browser) Type(ctx context.Context, selector, text string, add, submit bool) error

Type puts text in the element (replacing what's there unless add), and presses Enter after when submit.

type ConsoleLine

type ConsoleLine struct {
	Level string `json:"level"`
	Text  string `json:"text"`
}

ConsoleLine is one message of the page's console, or an uncaught error.

type Options

type Options struct {
	// Path is the browser to run; empty finds Chrome, Chromium, Edge or
	// Brave.
	Path string
	// Visible shows its window; by default it runs headless.
	Visible bool
	// Width and Height are the viewport (default 1280×800).
	Width, Height int
	// AllowLocal lets it reach loopback and private addresses (the user's
	// own app); link-local ones (cloud metadata) never.
	AllowLocal bool
	// PrivateHost reports whether host is one of the browser profile's own
	// sites, which may resolve to private addresses (an intranet over VPN
	// or split-horizon DNS) without AllowLocal; loopback still needs
	// AllowLocal, link-local never (BP-04a). Nil: none is.
	PrivateHost func(host string) bool
	// Host decides each host it would reach (deny_domains); nil allows all.
	Host func(host string) error
	// Navigation decides each top-level page it would load, other than
	// those Navigate is asked for (links, redirects, forms): nil allows
	// all.
	Navigation func(u *url.URL) error
	// Timeout bounds a page load (default 30s).
	Timeout time.Duration
	// KeepViewport leaves the page's size as it is (a view the user
	// watches, sized by the window) instead of Width×Height.
	KeepViewport bool
	// ProfileDir is a profile kept between runs (a browser profile's,
	// spec_049_browser_profiles BP-03): its sign-ins and cookies last, and
	// Close leaves it. Empty: a new, empty profile, removed on Close.
	ProfileDir string
}

Options configure a browser.

type Page

type Page struct {
	URL   string `json:"url"`
	Title string `json:"title"`
}

Page is where the browser is.

type Transport added in v0.8.4

type Transport interface {
	WriteJSON(v any) error
	ReadJSON(v any) error
	Close() error
}

Transport carries a DevTools Protocol connection's messages, whole, as JSON: a WebSocket to a browser Blitz started, or a relay to one someone else runs, such as the desktop app's own view (spec_050_agent_browser). ReadJSON blocks for the next message and fails once it's closed.

Directories

Path Synopsis
Package browsertest is a fake DevTools page, to test code driving a browser without one: it answers the commands package browser sends as a simple page would, and records them.
Package browsertest is a fake DevTools page, to test code driving a browser without one: it answers the commands package browser sends as a simple page would, and records them.

Jump to

Keyboard shortcuts

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