cdpclient

package
v0.0.0-...-4fa3274 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrOutcomeUnknown = errors.New("CDP command outcome is unknown")

ErrOutcomeUnknown means a CDP connection closed while a command was in flight, so the caller cannot know whether Chromium applied it.

Functions

func BrowserWebSocketURL

func BrowserWebSocketURL(ctx context.Context, versionURL string) (string, error)

BrowserWebSocketURL reads Chrome's browser-level DevTools WebSocket URL.

func DispatchStartURL

func DispatchStartURL(ctx context.Context, devtoolsURL, url string) error

DispatchStartURL closes extra page targets and dispatches a navigation on the first page target. It does not wait for lifecycle events; Chrome owns the eventual navigation result.

func DispatchStartURLAndWait

func DispatchStartURLAndWait(ctx context.Context, devtoolsURL, navigationURL, destination string) error

DispatchStartURLAndWait navigates through navigationURL and waits for destination to load without resolving to Chrome's network error page.

Types

type BrowserVersion

type BrowserVersion struct {
	ProtocolVersion string `json:"protocolVersion"`
	Product         string `json:"product"`
	Revision        string `json:"revision"`
	UserAgent       string `json:"userAgent"`
	JsVersion       string `json:"jsVersion"`
}

BrowserVersion is the result of a Browser.getVersion CDP call.

We use this struct only to confirm a successful round-trip; callers that just need a liveness probe can ignore the fields. The protocol-version fields are populated for convenience.

type Client

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

Client maintains one browser-level DevTools connection. Commands may be sent concurrently. Clients created by DialWithEvents also expose protocol events through Events.

func Dial

func Dial(ctx context.Context, devtoolsURL string) (*Client, error)

Dial opens a command-only DevTools connection. Protocol events are discarded, preserving the behavior expected by existing short-lived users.

func DialWithEvents

func DialWithEvents(ctx context.Context, devtoolsURL string) (*Client, error)

DialWithEvents opens a DevTools connection that delivers protocol events in receive order through Events.

func (*Client) Close

func (c *Client) Close() error

Close shuts down the WebSocket connection and unblocks pending commands.

func (*Client) CountPageTargets

func (c *Client) CountPageTargets(ctx context.Context) (int, error)

CountPageTargets returns the number of open page targets.

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done closes when the connection shuts down.

func (*Client) Events

func (c *Client) Events() <-chan Message

Events returns the event stream for clients created by DialWithEvents. It returns nil for command-only clients.

func (*Client) GetBrowserVersion

func (c *Client) GetBrowserVersion(ctx context.Context) (*BrowserVersion, error)

GetBrowserVersion sends Browser.getVersion on the browser-level DevTools endpoint. It is a cheap CDP round-trip that proves the WebSocket is connected to a live, CDP-responsive Chromium browser process.

Callers should use this after Dial as a readiness gate: a successful websocket.Dial alone is not enough because a dial can complete against a half-open socket of a killed Chromium, or against a freshly bound TCP listener of a Chromium that has not yet wired up its CDP routes. A Browser.getVersion round-trip rules out both cases.

func (*Client) GetExtensions

func (c *Client) GetExtensions(ctx context.Context) ([]ExtensionInfo, error)

GetExtensions returns all unpacked extensions known to Chromium.

func (*Client) GetHistograms

func (c *Client) GetHistograms(ctx context.Context, query string) ([]Histogram, error)

GetHistograms sends Browser.getHistograms, a browser-level command that reads Chrome's in-memory UMA histograms without attaching to any page. query is a substring filter on the histogram name; empty returns all.

func (*Client) GetWindowBounds

func (c *Client) GetWindowBounds(ctx context.Context) (WindowBounds, error)

GetWindowBounds queries the OS window bounds for the first page target via Browser.getWindowForTarget. It's a one-shot read; callers that need to wait for the WM to settle should poll this.

func (*Client) IsClosed

func (c *Client) IsClosed() bool

IsClosed reports whether the connection has shut down.

func (*Client) LoadUnpackedExtension

func (c *Client) LoadUnpackedExtension(ctx context.Context, path string) (string, error)

LoadUnpackedExtension installs an unpacked extension from an absolute path visible to Chromium and returns its extension ID.

func (*Client) Send

func (c *Client) Send(ctx context.Context, method string, params any, sessionID string) (json.RawMessage, error)

Send sends a CDP command and waits for its matching response.

func (*Client) SetDeviceMetricsOverride

func (c *Client) SetDeviceMetricsOverride(ctx context.Context, width, height int) error

SetDeviceMetricsOverride sets the viewport dimensions on the first page target found in the browser. It attaches to the target with a flattened session, sends Emulation.setDeviceMetricsOverride, then detaches.

func (*Client) SetWindowBoundsMaximized

func (c *Client) SetWindowBoundsMaximized(ctx context.Context) error

SetWindowBoundsMaximized puts the OS window backing the first page target into the maximized state via Browser.setWindowBounds. It is idempotent — invoking it on a window already in maximized state is a no-op.

A mutter-managed window in maximized state auto-tracks RANDR resizes (the WM reflows it to fill the new root). So after a display resize the server only has to make sure the window is in maximized state; mutter does the rest. This replaces the prior approach of restarting chromium so it could re-apply --start-maximized.

We intentionally avoid the explicit-bounds form of setWindowBounds ({left, top, width, height} with windowState:"normal"): once a window is in normal state it stops auto-tracking subsequent RANDR events.

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

Error is a protocol error returned by Chromium.

func (*Error) Error

func (e *Error) Error() string

type ExtensionInfo

type ExtensionInfo struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Version string `json:"version"`
	Path    string `json:"path"`
	Enabled bool   `json:"enabled"`
}

ExtensionInfo describes an unpacked extension known to Chromium.

type Histogram

type Histogram struct {
	Name    string            `json:"name"`
	Sum     int64             `json:"sum"`
	Count   int64             `json:"count"`
	Buckets []HistogramBucket `json:"buckets"`
}

Histogram is a snapshot of a Chrome UMA histogram as returned by Browser.getHistograms. Values are cumulative since browser start and the units follow the UMA definition of the histogram (PageLoad timings are milliseconds). Only buckets with at least one sample are present.

type HistogramBucket

type HistogramBucket struct {
	Low   int64 `json:"low"`
	High  int64 `json:"high"`
	Count int64 `json:"count"`
}

HistogramBucket is a [Low, High) bucket of a Histogram.

type Message

type Message struct {
	ID        int64           `json:"id,omitempty"`
	Method    string          `json:"method,omitempty"`
	Params    json.RawMessage `json:"params,omitempty"`
	Result    json.RawMessage `json:"result,omitempty"`
	Error     *Error          `json:"error,omitempty"`
	SessionID string          `json:"sessionId,omitempty"`
}

Message is a response or event received from Chromium.

type WindowBounds

type WindowBounds struct {
	WindowID    int
	Width       int
	Height      int
	WindowState string
}

WindowBounds is the subset of Browser.getWindowBounds CDP returns that callers care about. For maximized/fullscreen windows the width/height fields reflect the live window size (which the WM aligns with the X root); for normal-state windows they reflect the saved-restore bounds.

Jump to

Keyboard shortcuts

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