browser

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 19 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_parity_027 §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 PublicAddr

func PublicAddr(addr netip.Addr) bool

PublicAddr reports whether addr is a routable public address: not loopback, private, link-local (cloud metadata), CGNAT, multicast or unspecified.

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 Launch

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

Launch starts a browser with a new, empty profile.

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 and removes its profile.

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
	// 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
}

Options configure a browser.

type Page

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

Page is where the browser is.

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