browser

package
v0.11.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0, MIT Imports: 13 Imported by: 0

Documentation

Overview

Package browser provides an agent-driven headless Chrome wrapper built on chromedp. A single Browser holds one chromedp session against a long-lived Chrome process; tools call into it through the Manager singleton.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Browser

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

Browser wraps a chromedp session. Methods are safe to call from a single goroutine; the underlying chromedp context serializes navigation and DOM access. Use Manager when you need cross-tool coordination.

func New

func New(opts Options) *Browser

New constructs a Browser with the given options. Options.Headless defaults to true when zero-value (Open will set it explicitly).

func (*Browser) Click

func (b *Browser) Click(selector string) error

Click clicks the first element matching selector.

func (*Browser) Close

func (b *Browser) Close()

Close tears down the chromedp session and the Chrome process. Idempotent.

func (*Browser) Eval

func (b *Browser) Eval(jsExpr string) (any, error)

Eval runs an arbitrary JS expression and decodes the result via JSON.

func (*Browser) Fill

func (b *Browser) Fill(selector, value string) error

Fill clears and types value into the first element matching selector.

func (*Browser) IsOpen

func (b *Browser) IsOpen() bool

IsOpen reports whether the underlying browser is currently spawned.

func (*Browser) Markdown

func (b *Browser) Markdown() (string, error)

Markdown extracts the main content of the page and returns a tiny markdown rendering. Heuristic: prefer <main> > <article> > <body>.

func (*Browser) Navigate

func (b *Browser) Navigate(url string) error

Navigate loads the given URL. Waits for ready state.

func (*Browser) Open

func (b *Browser) Open(parent context.Context) error

Open spawns the underlying Chrome process and waits until it is ready. It is safe to call Open more than once; subsequent calls are no-ops.

func (*Browser) OuterHTML

func (b *Browser) OuterHTML(selector string) (string, error)

OuterHTML returns the outerHTML of the first element matching selector.

func (*Browser) Screenshot

func (b *Browser) Screenshot(width, height int) ([]byte, error)

Screenshot captures a PNG of the current viewport. width/height ≤0 use the chromedp default viewport.

func (*Browser) ScreenshotElement

func (b *Browser) ScreenshotElement(selector string) ([]byte, error)

ScreenshotElement captures a PNG of a single CSS-selected element.

func (*Browser) Select

func (b *Browser) Select(selector, value string) error

Select picks value from a <select> element.

func (*Browser) Text

func (b *Browser) Text(selector string) (string, error)

Text extracts visible text from the first element matching selector. An empty selector defaults to "body".

func (*Browser) Title

func (b *Browser) Title() (string, error)

Title returns the current page title.

func (*Browser) URL

func (b *Browser) URL() (string, error)

URL returns the current page URL.

func (*Browser) WaitForNavigation

func (b *Browser) WaitForNavigation(timeout time.Duration) error

WaitForNavigation waits until the body of the next page is ready. Useful after triggering a click that initiates navigation.

func (*Browser) WaitForSelector

func (b *Browser) WaitForSelector(selector string, timeout time.Duration) error

WaitForSelector waits for the given selector to appear in the DOM.

type Manager

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

Manager owns the singleton browser session for an overkill process. Lazy-spawns Chrome on the first call to Get and tears it down via Shutdown.

func NewManager

func NewManager(opts Options) *Manager

NewManager constructs a Manager bound to the given options. The underlying Browser is not spawned until Get is called.

func (*Manager) Close

func (m *Manager) Close()

Close tears down the browser if it is open.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context) (*Browser, error)

Get returns the live Browser, opening it on first call.

func (*Manager) IsActive

func (m *Manager) IsActive() bool

IsActive reports whether the manager has a live, open browser. Cheap and safe to poll from the TUI.

func (*Manager) Refresh

func (m *Manager) Refresh() error

Refresh reloads the current page.

func (*Manager) Status

func (m *Manager) Status() Status

Status snapshots the current state. Best-effort: memory comes from /proc/<pid>/status on Linux and is zero elsewhere.

type Options

type Options struct {
	// Headless=true is retained for existing callers. False uses the default
	// headless mode; use Visible to explicitly open a visible window.
	Headless   bool
	Visible    bool
	ChromePath string
	UserAgent  string
	// Timeout caps each launch/action; zero retains operation defaults.
	Timeout time.Duration
}

Options configures the browser process. An empty Options is valid and uses chromedp defaults (headless, auto-detected Chrome path).

type Status

type Status struct {
	Running   bool
	PID       int
	MemoryKB  int64
	URL       string
	Title     string
	Headless  bool
	UserAgent string
}

Status describes the current browser state for the /browser dialog.

Directories

Path Synopsis
Package devbrowser provides a narrow structured Chrome interface.
Package devbrowser provides a narrow structured Chrome interface.

Jump to

Keyboard shortcuts

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