CDPSession 类
🌐 CDPSession class
CDPSession 实例用于直接与 Chrome Devtools 协议进行交互。
🌐 The CDPSession instances are used to talk raw Chrome Devtools Protocol.
语法
🌐 Signature
export declare abstract class CDPSession extends EventEmitter<CDPSessionEvents>
Extends: EventEmitter<CDPSessionEvents>
附注
🌐 Remarks
协议方法可以通过 CDPSession.send() 方法调用,协议事件可以通过 CDPSession.on 方法订阅。
🌐 Protocol methods can be called with CDPSession.send() method and protocol events can be subscribed to with CDPSession.on method.
有用的链接:DevTools 协议查看器 和 DevTools 协议入门。
🌐 Useful links: DevTools Protocol Viewer and Getting Started with DevTools Protocol.
这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 CDPSession 类的子类。
🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the CDPSession class.
示例
🌐 Example
const client = await page.createCDPSession();
await client.send('Animation.enable');
client.on('Animation.animationCreated', () =>
console.log('Animation created!'),
);
const response = await client.send('Animation.getPlaybackRate');
console.log('playback rate is ' + response.playbackRate);
await client.send('Animation.setPlaybackRate', {
playbackRate: response.playbackRate / 2,
});
属性
🌐 Properties
属性 🌐 Property | 修饰符 🌐 Modifiers | 类型 🌐 Type | 描述 🌐 Description |
|---|---|---|---|
| detached |
| boolean | 如果会话已分离,则为 True,否则为 false。 🌐 True if the session has been detached, false otherwise. |
方法
🌐 Methods
方法 🌐 Method | 修饰符 🌐 Modifiers | 描述 🌐 Description |
|---|---|---|
| connection() | 此会话的底层连接(如果有)。 🌐 The underlying connection for this session, if any. | |
| detach() | 将 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. | |
| id() | 返回会话的 ID。 🌐 Returns the session's id. | |
| send(method, params, options) |