cdp
5 分钟阅读
原文:https://pkg.go.dev/github.com/go-rod/rod/lib/cdp
收录该文档时间:
2024-11-20T18:02:07+08:00
This client is directly based on this doc.
该客户端直接基于此 文档。
You can treat it as a minimal example of how to use the DevTools Protocol, no complex abstraction.
您可以将其视为使用 DevTools 协议的最小示例,无复杂抽象。
It’s thread-safe, and context first.
它是线程安全的,并以上下文为先。
For basic usage, check this file.
有关基本用法,请查看此 文件。
个人添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package cdp_test import ( "context" "fmt" "github.com/go-rod/rod/lib/cdp" "github.com/go-rod/rod/lib/launcher" "github.com/go-rod/rod/lib/proto" "github.com/go-rod/rod/lib/utils" "github.com/ysmood/gson" ) func ExampleClient() { ctx := context.Background() // launch a browser url := launcher.New().MustLaunch() // create a controller client := cdp.New().Start(cdp.MustConnectWS(url)) go func() { for range client.Event() { // you must consume the events utils.Noop() } }() // 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(gson.New(res).Get("targetId").Str())) // close browser by using the proto lib to encode json _ = proto.BrowserClose{}.Call(client) // Output: 32 } func Example_customize_cdp_log() { ws := cdp.MustConnectWS(launcher.New().MustLaunch()) cdp.New(). Logger(utils.Log(func(args ...interface{}) { switch v := args[0].(type) { case *cdp.Request: fmt.Printf("id: %d", v.ID) } })). Start(ws) }
For more info, check the unit tests.
有关更多信息,请查看单元测试。
Package cdp for application layer communication with browser.
Package cdp 用于与浏览器的应用层通信。
Example (Customize_cdp_log)
|
|
常量
This section is empty.
变量
|
|
ErrCtxDestroyed type.
ErrCtxDestroyed 类型。
|
|
ErrCtxNotFound type.
ErrCtxNotFound 类型。
|
|
ErrNodeNotFoundAtPos type.
ErrNodeNotFoundAtPos 类型。
|
|
ErrNotAttachedToActivePage type.
ErrNotAttachedToActivePage 类型。
|
|
ErrObjNotFound type.
ErrObjNotFound 类型。
|
|
ErrSearchSessionNotFound type.
ErrSearchSessionNotFound 类型。
|
|
ErrSessionNotFound type.
ErrSessionNotFound 类型。
函数
This section is empty.
类型
type BadHandshakeError <-0.114.8
|
|
BadHandshakeError type.
BadHandshakeError 类型。
(*BadHandshakeError) Error <-0.114.8
|
|
type Client
|
|
Client is a devtools protocol connection instance.
Client 是 DevTools 协议连接实例。
Example
|
|
func MustStartWithURL <-0.106.0
|
|
MustStartWithURL helper for ConnectURL.
MustStartWithURL 是 ConnectURL 的辅助函数。
func New
|
|
New creates a cdp connection, all messages from Client.Event must be received or they will block the client.
New 创建一个 cdp 连接,必须接收 Client.Event 中的所有消息,否则将阻塞客户端。
func StartWithURL <-0.106.0
|
|
StartWithURL helper to connect to the u with the default websocket lib.
StartWithURL 是连接到 u 的辅助函数,使用默认的 WebSocket 库。
(*Client) Call
|
|
Call a method and wait for its response.
调用方法并等待响应。
(*Client) Event
|
|
Event returns a channel that will emit browser devtools protocol events. Must be consumed or will block producer.
Event 返回一个通道,将发出浏览器 DevTools 协议事件。必须消费,否则会阻塞生产者。
(*Client) Logger <-0.70.0
|
|
Logger sets the logger to log all the requests, responses, and events transferred between Rod and the browser. The default format for each type is in file format.go.
Logger 设置日志记录器,用于记录在 Rod 和浏览器之间传输的所有请求、响应和事件。每种类型的默认格式位于文件 format.go 中。
(*Client) Start <-0.106.0
|
|
Start to browser.
启动到浏览器的连接。
type Dialer <-0.75.0
|
|
Dialer interface for WebSocket connection.
Dialer 是用于 WebSocket 连接的接口。
type Error
|
|
Error of the Response.
Response 的错误类型。
(*Error) Error
|
|
Error stdlib interface.
Error 实现标准库接口。
(Error) Is <-0.74.0
|
|
Is stdlib interface.
Is 实现标准库接口。
type Event
|
|
Event from browser.
浏览器事件。(Event) String <-0.70.0
|
|
type Request
|
|
Request to send to browser.
发送到浏览器的请求。
(Request) String <-0.70.0
|
|
type Response <-0.49.6
|
|
Response from browser.
来自浏览器的响应。
(Response) String <-0.70.0
|
|
type WebSocket <-0.75.0
|
|
WebSocket client for chromium. It only implements a subset of WebSocket protocol. Both the Read and Write are thread-safe. Limitation: https://bugs.chromium.org/p/chromium/issues/detail?id=1069431 Ref: https://tools.ietf.org/html/rfc6455
用于 Chromium 的 WebSocket 客户端。仅实现 WebSocket 协议的一个子集。Read 和 Write 均为线程安全。限制:https://bugs.chromium.org/p/chromium/issues/detail?id=1069431 参考:https://tools.ietf.org/html/rfc6455
(*WebSocket) Close <-0.106.0
|
|
Close the underlying connection.
关闭底层连接。
(*WebSocket) Connect <-0.75.0
|
|
Connect to browser.
连接到浏览器。
(*WebSocket) Read <-0.75.0
|
|
Read a message from browser.
从浏览器读取消息。
(*WebSocket) Send <-0.75.0
|
|
Send a message to browser. Because we use zero-copy design, it will modify the content of the msg. It won’t allocate new memory.
发送消息到浏览器。由于我们使用零拷贝设计,它会修改 msg 的内容,不会分配新内存。
type WebSocketable <-0.78.0
|
|
WebSocketable enables you to choose the websocket lib you want to use. Such as you can easily wrap gorilla/websocket and use it as the transport layer.
WebSocketable 允许您选择所需的 WebSocket 库。例如,您可以轻松包装 gorilla/websocket 并将其用作传输层。
func MustConnectWS <-0.106.0
|
|
MustConnectWS helper to make a websocket connection.
MustConnectWS 是建立 WebSocket 连接的辅助函数。