browserx

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 22 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

Overview

Package browserx drives a Chromium browser over the DevTools protocol, with the behaviour of Playwright's page API where callers depend on it: auto-waiting for visible/enabled/editable elements, Playwright's key names and selector extensions, load states and screenshots. It is built on chromedp and needs no Node runtime.

Index

Constants

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is Playwright's default for navigations and actions.

Variables

This section is empty.

Functions

func EndpointAvailable

func EndpointAvailable(ctx context.Context, endpoint string) bool

EndpointAvailable reports whether a DevTools HTTP endpoint such as http://127.0.0.1:9222 answers /json/version.

func FirstLine

func FirstLine(s string) string

FirstLine returns the first line of an error message, for callers that show only the headline of a page-side exception.

func HasKey

func HasKey(s string) bool

HasKey reports whether the US layout can press s as one key (a single character or a key name); other text must be inserted instead.

func StartDetached

func StartDetached(executable string, args, env []string) error

StartDetached starts a program in its own session so it outlives the caller and is not killed with its process group. Its output is discarded. env replaces the environment when non-nil. The process is released, never waited on.

func WaitForEndpoint

func WaitForEndpoint(ctx context.Context, endpoint string, timeout time.Duration) (bool, error)

WaitForEndpoint polls EndpointAvailable until it answers or the timeout passes. A cancelled ctx returns its error.

Types

type Box

type Box struct {
	X, Y, Width, Height float64
}

Box is an element's bounding box in main-frame viewport coordinates.

type Browser

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

Browser is a connection to a running Chromium (its default browser context).

func Connect

func Connect(ctx context.Context, endpoint string) (*Browser, error)

Connect attaches to the browser behind a DevTools endpoint (http://host:port or a ws:// URL). It opens no tab. Close detaches without closing the browser.

func (*Browser) Close

func (b *Browser) Close()

Close drops the connection and leaves the browser, and its tabs, running.

func (*Browser) Connected

func (b *Browser) Connected() bool

Connected reports whether the connection is still usable.

func (*Browser) Cookies

func (b *Browser) Cookies(ctx context.Context) ([]Cookie, error)

Cookies returns every cookie of the default browser context, shaped like Playwright's context.cookies().

func (*Browser) Done

func (b *Browser) Done() <-chan struct{}

Done is closed when the connection to the browser is gone.

func (*Browser) NewPage

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

NewPage opens a blank tab.

func (*Browser) OnNewPage

func (b *Browser) OnNewPage(fn func(*Page))

OnNewPage registers a callback for tabs that open after the call (popups, target=_blank links, NewPage). The page is attached before fn runs. fn runs on its own goroutine.

func (*Browser) Pages

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

Pages lists the open tabs of the default browser context in the order the browser reports them.

type Cookie struct {
	Name     string  `json:"name"`
	Value    string  `json:"value"`
	Domain   string  `json:"domain"`
	Path     string  `json:"path"`
	Expires  float64 `json:"expires"`
	HTTPOnly bool    `json:"httpOnly"`
	Secure   bool    `json:"secure"`
	SameSite string  `json:"sameSite"`
}

Cookie is the cookie shape Playwright returns, in its key order.

type ExceptionError

type ExceptionError struct{ Message string }

ExceptionError is a JavaScript exception thrown by evaluated code.

func (*ExceptionError) Error

func (e *ExceptionError) Error() string

type GotoOptions

type GotoOptions struct {
	WaitUntil string
	Timeout   time.Duration
	Referer   string
}

GotoOptions tune Goto. WaitUntil is commit, domcontentloaded, load or networkidle (default load, as in Playwright).

type Keyboard

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

Keyboard dispatches key events with Playwright's key names and modifier bookkeeping.

func (*Keyboard) Down

func (k *Keyboard) Down(ctx context.Context, key string) error

Down presses a key without releasing it.

func (*Keyboard) InsertText

func (k *Keyboard) InsertText(ctx context.Context, text string) error

InsertText inserts text as if pasted: no key events are generated.

func (*Keyboard) Press

func (k *Keyboard) Press(ctx context.Context, combo string, delay time.Duration) error

Press presses and releases a key or a combination such as "Control+A", holding the modifiers in between. delay is the pause between down and up.

func (*Keyboard) Type

func (k *Keyboard) Type(ctx context.Context, text string, delay time.Duration) error

Type types text one character at a time: characters the US layout has are pressed as keys, anything else is inserted. delay pauses before each character, except after the last.

func (*Keyboard) Up

func (k *Keyboard) Up(ctx context.Context, key string) error

Up releases a key.

type Mouse

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

Mouse dispatches mouse events at tracked coordinates, like Playwright's page.mouse. It starts at (0, 0).

func (*Mouse) Down

func (m *Mouse) Down(ctx context.Context) error

Down presses the left button at the current position.

func (*Mouse) Move

func (m *Mouse) Move(ctx context.Context, x, y float64) error

Move moves the pointer to (x, y).

func (*Mouse) Up

func (m *Mouse) Up(ctx context.Context) error

Up releases the left button at the current position.

func (*Mouse) Wheel

func (m *Mouse) Wheel(ctx context.Context, deltaX, deltaY float64) error

Wheel scrolls at the current position.

type Page

type Page struct {
	Mouse    *Mouse
	Keyboard *Keyboard
	// contains filtered or unexported fields
}

Page is one tab.

func (*Page) AddInitScript

func (p *Page) AddInitScript(ctx context.Context, source string) error

AddInitScript runs source in every document the tab loads from now on, before the page's own scripts, and in the document that is already loaded.

func (*Page) Close

func (p *Page) Close(ctx context.Context) error

Close closes the tab.

func (*Page) Closed

func (p *Page) Closed() bool

Closed reports whether the tab is gone or the connection dropped.

func (*Page) Content

func (p *Page) Content(ctx context.Context) (string, error)

Content is the page's HTML including its doctype, like Playwright's page.content().

func (*Page) Eval

func (p *Page) Eval(ctx context.Context, expression string) (json.RawMessage, error)

Eval evaluates a JavaScript expression in the page's main world, waits for a returned promise, and returns the result by value as JSON. A result of undefined comes back as the JSON value null.

func (*Page) EvalJSON

func (p *Page) EvalJSON(ctx context.Context, expression string, out any) error

EvalJSON is Eval followed by decoding the result into out.

func (*Page) EvalWithSelectors

func (p *Page) EvalWithSelectors(ctx context.Context, body string, out any, args ...any) error

EvalWithSelectors evaluates the statements in body as an async function body with L (the selector helper: queryAll, visible, ...) and the JSON encoded args in scope. The body returns the value to report, e.g.

body: `return L.queryAll(args[0]).length`

func (*Page) Fill

func (p *Page) Fill(ctx context.Context, selector, value string, timeout time.Duration) error

Fill sets the value of the single element matching selector with Playwright's locator.fill semantics: it waits for the element to be attached, visible, enabled and editable, then selects its content and replaces it. An empty value clears the field.

func (*Page) Goto

func (p *Page) Goto(ctx context.Context, url string, o GotoOptions) (int, error)

Goto navigates and waits for the load state, returning the main document's HTTP status (0 for a navigation without a network response, such as a hash change or about:blank).

func (*Page) ID

func (p *Page) ID() string

ID is the DevTools target id of the tab.

func (*Page) PressOn

func (p *Page) PressOn(ctx context.Context, selector, key string, timeout time.Duration) error

PressOn focuses the single element matching selector and presses a key, like locator.press.

func (*Page) PressSequentially

func (p *Page) PressSequentially(ctx context.Context, selector, text string, delay, timeout time.Duration) error

PressSequentially focuses the single element matching selector and types text into it key by key, like locator.pressSequentially.

func (*Page) Reload

func (p *Page) Reload(ctx context.Context, o GotoOptions) error

Reload reloads the tab and waits like Goto.

func (*Page) Screenshot

func (p *Page) Screenshot(ctx context.Context, o ScreenshotOptions) ([]byte, error)

Screenshot captures the viewport, the full page, or one element. When the selector matches nothing the whole page is captured instead.

func (*Page) ScrollAndBox

func (p *Page) ScrollAndBox(ctx context.Context, selector string) (box *Box, found bool, err error)

ScrollAndBox scrolls the first element matching selector into view (when it is not already) and returns its bounding box. found is false when nothing matches; box is nil when the element has no layout.

func (*Page) ScrollAndBoxExpr

func (p *Page) ScrollAndBoxExpr(ctx context.Context, elementExpr string) (box *Box, found bool, err error)

ScrollAndBoxExpr is ScrollAndBox for an element chosen by a JavaScript expression (with L in scope) that yields an Element or undefined.

func (*Page) Title

func (p *Page) Title(ctx context.Context) (string, error)

Title is document.title.

func (*Page) URL

func (p *Page) URL(ctx context.Context) (string, error)

URL is the committed URL of the tab.

func (*Page) WaitForLoadState

func (p *Page) WaitForLoadState(ctx context.Context, state string, timeout time.Duration) error

WaitForLoadState waits until the current document reached the state (load, domcontentloaded or networkidle).

func (*Page) WaitForSelector

func (p *Page) WaitForSelector(ctx context.Context, selector string, timeout time.Duration) error

WaitForSelector waits until the first element matching selector is visible.

type ScreenshotOptions

type ScreenshotOptions struct {
	Format   string
	Quality  int
	FullPage bool
	// Selector captures one element, scrolled into view first.
	Selector string
}

ScreenshotOptions tune Screenshot. Format is "png" (default) or "jpeg".

type StrictError

type StrictError struct {
	Selector string
	Count    int
}

StrictError reports a selector that matched several elements where one was required, as Playwright's strict mode does.

func (*StrictError) Error

func (e *StrictError) Error() string

type TimeoutError

type TimeoutError struct {
	Op      string
	Timeout time.Duration
}

TimeoutError reports an operation that did not finish in time, with Playwright's wording ("page.goto: Timeout 30000ms exceeded.").

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Jump to

Keyboard shortcuts

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