cdp

package
v1.87.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package cdp speaks the Chrome DevTools Protocol to a browser's DevTools WebSocket endpoint: one JSON object per message, commands answered by id, events in between.

The endpoint is the one --remote-debugging-port opens, given a port number. The other transport Chrome has, --remote-debugging-pipe, would need no WebSocket, but a browser started with it -- or with port 0 -- declares itself automated to every page (navigator.webdriver is true), and a challenge page reads that first.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHandshake       = errors.New("websocket handshake failed")
	ErrProtocol        = errors.New("websocket protocol error")
	ErrMessageTooLarge = errors.New("websocket message too large")
)
View Source
var ErrClosed = errors.New("cdp connection closed")

Functions

func Click

func Click(ctx context.Context, caller Caller, sessionId string, x float64, y float64) error

Click moves the mouse to the point from somewhere below and to its left and presses it there, at the pace of a hand rather than in one event.

func IsTransient

func IsTransient(err error) bool

IsTransient reports whether the error is the browser declining a command for now -- a document being replaced has no nodes for a moment -- rather than the page or the connection being gone, which waiting does not mend.

func Pause

func Pause(ctx context.Context, least time.Duration, spread time.Duration)

Pause waits between least and least+spread, or until the context ends.

Types

type Caller

type Caller interface {
	Call(ctx context.Context, sessionId string, method string, params any, result any) error
}

Caller sends a command and decodes its result; *Conn is one, and a test's fake browser another.

type Conn

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

Conn is one connection to a browser. Commands may be sent from several goroutines; events are read and dropped, since nothing here waits on one.

func NewConn

func NewConn(transport Transport) (*Conn, error)

NewConn starts reading the browser's messages and returns the connection. Reading ends, and every pending and later command fails, when the transport does.

func (*Conn) Call

func (c *Conn) Call(ctx context.Context, sessionId string, method string, params any, result any) error

Call sends a command and decodes its result into result, which may be nil. An empty session id addresses the browser itself; a page is addressed through the session Target.attachToTarget returned for it.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the transport, which ends the connection.

func (*Conn) Done

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

Done is closed when the connection has ended.

type ProtocolError

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

ProtocolError is an error the browser answered a command with.

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

type Transport

type Transport interface {
	ReadMessage() ([]byte, error)
	WriteMessage(data []byte) error
	Close() error
}

Transport carries whole messages; *WebSocket is one.

type WebSocket

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

WebSocket is one client connection.

func DialWebSocket

func DialWebSocket(ctx context.Context, rawUrl string) (*WebSocket, error)

DialWebSocket opens a WebSocket to a ws:// URL.

func (*WebSocket) Close

func (w *WebSocket) Close() error

Close ends the connection without the closing handshake; the browser it is to is about to be closed anyway.

func (*WebSocket) ReadMessage

func (w *WebSocket) ReadMessage() ([]byte, error)

ReadMessage returns the next text or binary message, answering pings on the way. A close from the server ends the connection with io.EOF.

func (*WebSocket) WriteMessage

func (w *WebSocket) WriteMessage(data []byte) error

WriteMessage sends one text message.

Directories

Path Synopsis
Package chrome starts a headed Chrome and connects to it over the DevTools protocol, the way a page behind a bot check needs it driven.
Package chrome starts a headed Chrome and connects to it over the DevTools protocol, the way a page behind a bot check needs it driven.
Package turnstile passes Cloudflare's Turnstile the way a person does: when its widget asks whether the browser is human, it clicks the box.
Package turnstile passes Cloudflare's Turnstile the way a person does: when its widget asks whether the browser is human, it clicks the box.

Jump to

Keyboard shortcuts

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