CDPSession
CDPSession
实例用于讨论原始 Chrome Devtools 协议:
¥The CDPSession
instances are used to talk raw Chrome Devtools Protocol:
-
协议方法可以通过
session.send
方法调用。¥protocol methods can be called with
session.send
method. -
可以使用
session.on
方法订阅协议事件。¥protocol events can be subscribed to with
session.on
method.
有用的链接:
¥Useful links:
-
有关 DevTools 协议的文档可以在这里找到:DevTools 协议查看器。
¥Documentation on DevTools Protocol can be found here: DevTools Protocol Viewer.
-
DevTools 协议入门:https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
¥Getting Started with DevTools Protocol: https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
CDPSession client = page.context().newCDPSession(page);
client.send("Runtime.enable");
client.on("Animation.animationCreated", (event) -> System.out.println("Animation created!"));
JsonObject response = client.send("Animation.getPlaybackRate");
double playbackRate = response.get("playbackRate").getAsDouble();
System.out.println("playback rate is " + playbackRate);
JsonObject params = new JsonObject();
params.addProperty("playbackRate", playbackRate / 2);
client.send("Animation.setPlaybackRate", params);
方法
¥Methods
detach
Added before v1.9从目标分离 CDPSession。一旦分离,CDPSession 对象将不会触发任何事件,也不能用于发送消息。
¥Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to send messages.
用法
¥Usage
CDPSession.detach();
返回
¥Returns
off
Added in: v1.37为指定事件名称的事件取消注册事件处理程序。对于指定名称的事件,将不再调用指定的处理程序。
¥Unregister an event handler for events with the specified event name. The given handler will not be called anymore for events with the given name.
用法
¥Usage
CDPSession.off(eventName, handler);
参数
¥Arguments
CDP 事件名称。
¥CDP event name.
handler
Consumer<JsonObject>#
事件处理程序。
¥Event handler.
on
Added in: v1.37为指定事件名称的事件注册一个事件处理程序。每个指定名称的事件都将调用指定的处理程序。
¥Register an event handler for events with the specified event name. The given handler will be called for every event with the given name.
用法
¥Usage
CDPSession.on(eventName, handler);
参数
¥Arguments
CDP 事件名称。
¥CDP event name.
handler
Consumer<JsonObject>#
事件处理程序。
¥Event handler.
send
Added before v1.9用法
¥Usage
CDPSession.send(method);
CDPSession.send(method, args);
参数
¥Arguments
协议方法名称。
¥Protocol method name.
args
JsonObject (optional) Added in: v1.37#
可选方法参数。
¥Optional method parameters.
返回
¥Returns