cdp

package
v0.63.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: MIT Imports: 10 Imported by: 22

README

Overview

This client is directly based on doc

You can treat it as a minimal example of how to use the DevTools Protocol, no complex abstraction.

The lib is thread-safe, and context first. The overhead of encoding API will never be the bottleneck as long as you use headless browser.

For a basic example, check this file.

For a detailed example, check this file.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a devtools protocol connection instance.

Example
package main

import (
	"context"
	"fmt"

	"github.com/go-rod/rod/lib/cdp"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/utils"
	"github.com/tidwall/gjson"
)

func main() {
	ctx := context.Background()

	// launch a browser
	url := launcher.New().MustLaunch()

	// create a controller
	client := cdp.New(url).MustConnect(ctx)

	go func() {
		for range client.Event() {
			// you must consume the events
		}
	}()

	// Such as call this endpoint on the api doc:
	// https://chromedevtools.github.io/devtools-protocol/tot/Page#method-navigate
	// This will create a new tab and navigate to the test.com
	res, err := client.Call(ctx, "", "Target.createTarget", map[string]string{
		"url": "http://test.com",
	})
	utils.E(err)

	fmt.Println(len(gjson.ParseBytes(res).Get("targetId").Str))

	// close browser
	_, _ = client.Call(ctx, "", "Browser.close", nil)

}
Output:

32

func New

func New(websocketURL string) *Client

New creates a cdp connection, all messages from Client.Event must be received or they will block the client.

func (*Client) Call

func (cdp *Client) Call(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error)

Call a method and get its response, if ctx is nil context.Background() will be used

func (*Client) Connect

func (cdp *Client) Connect(ctx context.Context) error

Connect to browser

func (*Client) Debug

func (cdp *Client) Debug(enable bool) *Client

Debug is the flag to enable debug log to stdout.

func (*Client) DebugLog added in v0.46.5

func (cdp *Client) DebugLog(fn func(interface{})) *Client

DebugLog override the defaultDebugLog function

func (*Client) Event

func (cdp *Client) Event() <-chan *Event

Event returns a channel that will emit browser devtools protocol events. Must be consumed or will block producer.

func (*Client) Header

func (cdp *Client) Header(header http.Header) *Client

Header set the header of the remote control websocket request

func (*Client) MustConnect added in v0.50.0

func (cdp *Client) MustConnect(ctx context.Context) *Client

MustConnect to browser

func (*Client) Websocket

func (cdp *Client) Websocket(ws Websocketable) *Client

Websocket set the websocket lib to use

type DefaultWsClient

type DefaultWsClient struct {
	WriteBufferSize int
}

DefaultWsClient is the default websocket client

func NewDefaultWsClient added in v0.49.0

func NewDefaultWsClient() *DefaultWsClient

NewDefaultWsClient instance

func (*DefaultWsClient) Connect

func (c *DefaultWsClient) Connect(ctx context.Context, url string, header http.Header) (WebsocketableConn, error)

Connect interface

type DefaultWsConn

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

DefaultWsConn is the default websocket connection type

func (*DefaultWsConn) Read

func (c *DefaultWsConn) Read() (data []byte, err error)

Read a message

func (*DefaultWsConn) Send

func (c *DefaultWsConn) Send(data []byte) error

Send a message

type Error

type Error struct {
	Code    int64  `json:"code"`
	Message string `json:"message"`
	Data    string `json:"data"`
}

Error of the Response

func (*Error) Error

func (e *Error) Error() string

Error interface

type Event

type Event struct {
	SessionID string          `json:"sessionId,omitempty"`
	Method    string          `json:"method"`
	Params    json.RawMessage `json:"params,omitempty"`
}

Event from browser

type Request

type Request struct {
	ID        uint64      `json:"id"`
	SessionID string      `json:"sessionId,omitempty"`
	Method    string      `json:"method"`
	Params    interface{} `json:"params,omitempty"`
}

Request to send to browser

type Response added in v0.49.6

type Response struct {
	ID     uint64          `json:"id"`
	Result json.RawMessage `json:"result,omitempty"`
	Error  *Error          `json:"error,omitempty"`
}

Response from browser

type Websocketable

type Websocketable interface {
	// Connect to server
	Connect(ctx context.Context, url string, header http.Header) (WebsocketableConn, error)
}

Websocketable enables you to choose the websocket lib you want to use. By default cdp use github.com/gorilla/websocket

type WebsocketableConn

type WebsocketableConn interface {
	// Send text message only
	Send([]byte) error
	// Read returns text message only
	Read() ([]byte, error)
}

WebsocketableConn represents a connection session

Jump to

Keyboard shortcuts

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