browser

package
v0.86.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: AGPL-3.0 Imports: 34 Imported by: 0 modules, 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handle

type Handle struct {
	ID     string `json:"id"`
	URL    string `json:"url"`
	Title  string `json:"title"`
	Owner  string `json:"owner"`
	Headed bool   `json:"headed"`
}

Handle is what Open and OpenVisible return: the id later calls take, and enough for a card or a tool result to say which window it is.

type Interactive

type Interactive struct {
	Index   int    `json:"index"`
	Tag     string `json:"tag"`
	Type    string `json:"type,omitempty"`
	Role    string `json:"role,omitempty"`
	Text    string `json:"text"`
	Href    string `json:"href,omitempty"`
	Rect    Rect   `json:"rect"`
	Filled  *bool  `json:"filled,omitempty"`
	Checked *bool  `json:"checked,omitempty"`
}

Interactive is one clickable or fillable node from page-info. It never carries a control's value — filled/checked say that typing landed without putting the content in the transcript.

type Launcher

type Launcher func(dir string, headed bool) (Page, error)

Launcher starts a Chromium with its own user-data-dir. headed is a real window; false is headless. The directory is the caller's to delete.

type Page

type Page interface {
	Navigate(ctx context.Context, url string) error
	Info(ctx context.Context) (PageInfo, error)
	// HasText says whether text is in the page's visible text, the whole
	// document's and not only the viewport's.
	HasText(ctx context.Context, text string) (bool, error)
	// Settle waits until a navigation the last action may have started has
	// its document, bounded by domSettle.
	Settle(ctx context.Context) error
	// Click returns where it clicked, in CSS pixels.
	Click(ctx context.Context, t Target) (x, y float64, err error)
	Type(ctx context.Context, text string, clear bool, t Target) error
	Press(ctx context.Context, key string, t Target) error
	// Screenshot writes the viewport at one image pixel per CSS pixel and
	// returns its size.
	Screenshot(ctx context.Context, dest string) (w, h int, err error)
	Reload(ctx context.Context) error
	Back(ctx context.Context) error
	Forward(ctx context.Context) error
	// Scroll turns the wheel by dy CSS pixels at at, or at the viewport's
	// middle when at is nil, and returns where it turned it.
	Scroll(ctx context.Context, at *point, dy int) (x, y float64, err error)
	Focus(ctx context.Context) error
	// Cookies and SetCookies carry a browser's cookies, session ones included,
	// into the process that replaces it.
	Cookies(ctx context.Context) ([]*network.CookieParam, error)
	SetCookies(ctx context.Context, cookies []*network.CookieParam) error
	// Listen hands the page what to call with each screencast frame (base64
	// JPEG), with its URL and title whenever they change, with each step of a
	// download it starts, with each error its console shows, and with each
	// JavaScript dialog it opens, then nil once that dialog is closed, and
	// with its tabs whenever one opens, closes, comes to the front or changes
	// its URL or title.
	Listen(onFrame func(data string), onInfo func(url, title string), onDownload func(guid string, d api.BrowserDownload), onConsoleError func(text string), onDialog func(*api.BrowserDialog), onTabs func([]api.BrowserTab))
	// NewTab opens a blank tab in front; SelectTab brings the tab id to the
	// front; CloseTab closes it, unless it is the last one. Every other call
	// acts on the front tab.
	NewTab(ctx context.Context) error
	SelectTab(ctx context.Context, id string) error
	CloseTab(ctx context.Context, id string) error
	// AnswerDialog answers the open JavaScript dialog: OK with text (a
	// prompt's answer) when accept, Cancel otherwise. Every other call that
	// runs the page's script or input fails with errDialogOpen while one is
	// open.
	AnswerDialog(ctx context.Context, accept bool, text string) error
	// OnFileChooser hands the page what to call when a file input asks for
	// files, in the input's own mode; the paths it answers fill the input,
	// and none leaves it as it was.
	OnFileChooser(pick func(page.FileChooserOpenedMode) ([]string, error))
	Screencast(ctx context.Context, v Viewport) error
	StopScreencast(ctx context.Context) error
	Resize(ctx context.Context, v Viewport) error
	Mouse(ctx context.Context, m api.BrowserMouseOptions) error
	Key(ctx context.Context, k api.BrowserKeyOptions) error
	InsertText(ctx context.Context, text string) error
	// Selection is the text selected in the page, "" in a password field.
	Selection(ctx context.Context) (string, error)
	// ElementAt is the element under x,y, zero when the point holds none;
	// Locate is the box of the element each selector finds, zero for none,
	// after scrolling reveal's element into view when reveal is set.
	ElementAt(ctx context.Context, x, y float64) (api.BrowserElement, error)
	Locate(ctx context.Context, selectors []string, reveal string) ([]api.BrowserRect, error)
	Alive() bool
	Close() error
}

Page is one Chromium tab the service drives. chrome.go is the real one; tests inject a fake.

type PageInfo

type PageInfo struct {
	URL         string        `json:"url"`
	Title       string        `json:"title"`
	Text        string        `json:"text"`
	Interactive []Interactive `json:"interactive"`
	Truncated   bool          `json:"truncated"`
	// Downloads and the console errors are the service's to fill, never the
	// page script's.
	Downloads         []api.BrowserDownload `json:"-"`
	ConsoleErrors     []string              `json:"-"`
	ConsoleErrorsMore int                   `json:"-"`
	Dialogs           []api.BrowserDialog   `json:"-"`
}

PageInfo is what the agent reads instead of a screenshot: the URL, the visible text, and the interactive nodes it can click or type into.

type Rect

type Rect struct {
	X int `json:"x"`
	Y int `json:"y"`
	W int `json:"w"`
	H int `json:"h"`
}

Rect is an element's viewport box, integer pixels.

type Service

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

Service is one Chromium context per lich session: the window Browser tab opens and the page the agent drives are the same one. Headless until someone asks for a window; headed after OpenVisible.

func New

func New(emit func(name string, data any)) *Service

New starts a service that launches the system Chromium (chromium.FindBrowser) into an ephemeral profile, never the lich --app one, and tells the window about each browser through emit.

func (*Service) ActOnTab added in v0.83.0

ActOnTab is the agent opening, selecting or closing a tab, answered with the page then in front.

func (*Service) Answer added in v0.81.0

func (s *Service) Answer(_ context.Context, opts api.BrowserDialogOptions) (api.None, error)

Answer is the person answering the page's open dialog in the pane. The agent hears of it in its next page answer. A dialog the agent answered first leaves nothing to do, and that is no error.

func (*Service) AnswerDialog added in v0.81.0

AnswerDialog is the agent answering the page's open dialog, which the pane plays as its cursor pressing the dialog's button. It answers with the page as it stands after.

func (*Service) Attach

func (s *Service) Attach(owner string, v Viewport) (Handle, error)

Attach shows this session's browser in a pane sized v, starting a headless one at about:blank when there is none. The page the agent left stays as it is: the pane only starts watching it.

func (*Service) AttachView

func (s *Service) AttachView(_ context.Context, opts api.BrowserViewOptions) (api.BrowserHandle, error)

AttachView shows this session's page in a pane.

func (*Service) Back

func (s *Service) Back(id string) error

Back goes back in history.

func (*Service) Capture

Capture writes a PNG of this session's page and returns its path.

func (*Service) Cleanup

func (s *Service) Cleanup() error

Cleanup closes every browser this process started. Wired from main so a window-close exit does not leave Chromiums behind.

func (*Service) Click

func (s *Service) Click(id string, t Target) error

Click sends a real mouse event at the target, not element.click().

func (*Service) ClickAt

ClickAt clicks in this session's browser.

func (*Service) Close

func (s *Service) Close(id string) error

Close tears down one browser, by handle id or by the lich session that owns it.

func (*Service) CloseOne

func (s *Service) CloseOne(_ context.Context, opts api.SessionIDOptions) (api.None, error)

CloseOne tears down this session's browser.

func (*Service) CloseOwnedBy

func (s *Service) CloseOwnedBy(owner string) error

CloseOwnedBy tears down the browser of a lich session that is going away. Missing is not an error: most sessions never opened one.

func (*Service) DefaultView added in v0.81.0

func (s *Service) DefaultView(_ context.Context, opts api.BrowserDefaultViewOptions) (api.None, error)

DefaultView sets the size a page no pane shows lays out at.

func (*Service) Detach

func (s *Service) Detach(owner string) error

Detach stops the frames of a pane that closed or went off screen. The browser keeps running for the agent, at the window's size again rather than the pane's.

func (*Service) DetachView

func (s *Service) DetachView(_ context.Context, opts api.SessionIDOptions) (api.None, error)

DetachView stops a pane's frames.

func (*Service) ElementAtPoint added in v0.81.0

func (s *Service) ElementAtPoint(_ context.Context, opts api.BrowserPointOptions) (api.BrowserElement, error)

ElementAtPoint is the element under a point of this session's page.

func (*Service) Forward

func (s *Service) Forward(id string) error

Forward goes forward in history.

func (*Service) Go

Go loads a URL in this session's already-open browser.

func (*Service) GoBack

func (s *Service) GoBack(_ context.Context, opts api.SessionIDOptions) (api.None, error)

GoBack goes back in this session's history.

func (*Service) GoForward

func (s *Service) GoForward(_ context.Context, opts api.SessionIDOptions) (api.None, error)

GoForward goes forward in this session's history.

func (*Service) Handles

func (s *Service) Handles(_ context.Context, opts api.SessionIDOptions) ([]api.BrowserHandle, error)

Handles returns this session's browser, or an empty list.

func (*Service) Info

func (s *Service) Info(id string) (PageInfo, error)

Info returns the page the agent is looking at. Never includes form values.

func (*Service) Keep added in v0.84.0

func (s *Service) Keep(owner string) error

Keep answers the reaper's question about owner's browser with keep: it stays, and is asked about again once it sits idle another idle window.

func (*Service) KeepIdle added in v0.84.0

func (s *Service) KeepIdle(_ context.Context, opts api.SessionIDOptions) (api.None, error)

KeepIdle keeps this session's idle browser for another idle window.

func (*Service) KeyInput

func (s *Service) KeyInput(_ context.Context, opts api.BrowserKeyOptions) (api.None, error)

KeyInput passes one key from the pane.

func (*Service) List

func (s *Service) List(owner string) ([]Handle, error)

List returns this session's browser, or an empty list. Never null: a script should not have to tell those apart.

func (*Service) ListTabs added in v0.83.0

func (s *Service) ListTabs(_ context.Context, opts api.SessionIDOptions) ([]api.BrowserTab, error)

ListTabs is this session's browser's tabs.

func (*Service) LocateElements added in v0.81.0

func (s *Service) LocateElements(_ context.Context, opts api.BrowserLocateOptions) ([]api.BrowserRect, error)

LocateElements is where the elements opts.Selectors find stand now, after scrolling opts.Reveal's element into view when it is set.

func (*Service) MouseInput

func (s *Service) MouseInput(_ context.Context, opts api.BrowserMouseOptions) (api.None, error)

MouseInput passes one pointer event from the pane.

func (*Service) Nav

func (s *Service) Nav(owner, action, rawURL string) error

Nav runs the pane's toolbar: back, forward, reload, or go to url.

func (*Service) NavView

func (s *Service) NavView(_ context.Context, opts api.BrowserNavOptions) (api.None, error)

NavView runs the pane's toolbar.

func (*Service) Navigate

func (s *Service) Navigate(id, rawURL string) (Handle, error)

Navigate loads url in this session's browser.

func (*Service) Open

func (s *Service) Open(owner, rawURL string) (Handle, error)

Open navigates this session's browser to url, creating a headless one if none exists. A headed window the user already opened is reused, not replaced.

func (*Service) OpenURL

OpenURL navigates this session's browser, creating a headless one when the card has not opened a window.

func (*Service) OpenVisible

func (s *Service) OpenVisible(owner string) (Handle, error)

OpenVisible brings this session's browser on screen. No browser yet: a headed window at about:blank. Already headed: focus it. Headless only: reopen it as a window (promote).

func (*Service) Press

func (s *Service) Press(id, key string, t Target) error

Press sends one named key (Enter, Tab, Escape, …) as a real key event. An optional target is focused first, the same way Type focuses before typing.

func (*Service) PressKey

PressKey presses one named key in this session's browser.

func (*Service) Read

Read returns the page the agent is looking at, once the text it waits for came or went. A wait that gives up still answers with the page.

func (*Service) Reap

func (s *Service) Reap()

Reap asks the window, once each, whether to close the headless browsers no pane shows that have sat idle past the idle window. Close or Keep answers.

func (*Service) Reload

func (s *Service) Reload(id string) error

Reload reloads the current page.

func (*Service) ReloadPage

func (s *Service) ReloadPage(_ context.Context, opts api.SessionIDOptions) (api.None, error)

ReloadPage reloads this session's page.

func (*Service) Resize

func (s *Service) Resize(owner string, v Viewport) error

Resize lays the page out again at the pane's new size. A pane showing it gets its frames at that size too: the screencast keeps the size it started at, and the window would stretch the smaller frames over the larger pane.

func (*Service) ResizeView

func (s *Service) ResizeView(_ context.Context, opts api.BrowserViewOptions) (api.None, error)

ResizeView lays the page out at the pane's new size.

func (*Service) Running

func (s *Service) Running() []api.BrowserState

Running is every browser this process runs, for a window catching up.

func (*Service) RunningStates

func (s *Service) RunningStates(_ context.Context, _ api.None) ([]api.BrowserState, error)

RunningStates is every browser this process runs, for a window catching up.

func (*Service) Screenshot

func (s *Service) Screenshot(id, dest string) (string, error)

Screenshot writes a PNG to dest (created if empty: inside the profile dir) and returns that path. The bytes stay off the transcript.

func (*Service) Scroll

func (s *Service) Scroll(id string, dy int) error

Scroll moves the viewport by dy CSS pixels (positive is down). It turns the wheel where the agent last acted, else in the middle.

func (*Service) ScrollBy

func (s *Service) ScrollBy(_ context.Context, opts api.BrowserScrollOptions) (api.None, error)

ScrollBy scrolls this session's page.

func (*Service) SelectedText

func (s *Service) SelectedText(_ context.Context, opts api.SessionIDOptions) (string, error)

SelectedText is the text selected in this session's page.

func (*Service) SetDefaultView added in v0.81.0

func (s *Service) SetDefaultView(v Viewport)

SetDefaultView sets the size a headless page no pane shows lays out at, and lays out the running ones there. It is not a use: a window resized does not keep an idle browser from the reaper.

func (*Service) SetLocal added in v0.82.0

func (s *Service) SetLocal(isLocal func(raw string) bool, local func(owner, raw string) (string, error))

SetLocal sets how a local file, a file: URL or an absolute path, becomes a URL a session's browser may load: the http URL that serves it from that session's checkout (internal/preview). Unset, a local file is refused like any other blocked URL.

func (*Service) SetPicker added in v0.84.0

func (s *Service) SetPicker(pickFile func(title string) (string, error), pickFiles func(title string) ([]string, error))

SetPicker wires the host's file picker (internal/project), which a page's file input opens. Startup wiring, called before anything serves.

func (*Service) Show

Show brings this session's browser on screen.

func (*Service) TabAct added in v0.83.0

func (s *Service) TabAct(opts api.BrowserTabOptions) error

TabAct is the agent opening, selecting or closing a tab, which the pane plays as its cursor on the header's tabs. A new tab with a URL then loads it, as Navigate does.

func (*Service) TabNav added in v0.83.0

func (s *Service) TabNav(opts api.BrowserTabOptions) error

TabNav is the person opening, selecting or closing a tab in the pane.

func (*Service) TabView added in v0.83.0

func (s *Service) TabView(_ context.Context, opts api.BrowserTabOptions) (api.None, error)

TabView runs the pane's tabs.

func (*Service) Tabs added in v0.83.0

func (s *Service) Tabs(id string) ([]api.BrowserTab, error)

Tabs is this session's browser's tabs, in the pane's order.

func (*Service) TextInput

func (s *Service) TextInput(_ context.Context, opts api.BrowserTextOptions) (api.None, error)

TextInput types text the pane's input method committed.

func (*Service) Type

func (s *Service) Type(id, text string, clear bool, t Target) error

Type sends keystrokes. clear selects-and-replaces; the typed text never comes back through Info.

func (*Service) TypeText

func (s *Service) TypeText(_ context.Context, opts api.BrowserTypeOptions) (api.None, error)

TypeText types into this session's browser.

type Target

type Target struct {
	Index    *int     `json:"index,omitempty"`
	Selector string   `json:"selector,omitempty"`
	X        *float64 `json:"x,omitempty"`
	Y        *float64 `json:"y,omitempty"`
	// contains filtered or unexported fields
}

Target picks an element for click and type. Index is the one page-info stamped (data-lich-idx); selector is a CSS query; x/y are viewport coordinates. Coordinates win when set, then selector, then index.

type Viewport

type Viewport struct {
	W, H  int
	Scale float64
}

Viewport is the pane a page is drawn into: W and H in CSS pixels, Scale the device pixels per CSS pixel.

Jump to

Keyboard shortcuts

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