类:Debugger
类:Debugger
Chrome 远程调试协议的替代传输方式。
进程:主进程
此类未从 'electron'
模块导出。它仅作为 Electron API 中其他方法的返回值可用。
Chrome Developer Tools 在 JavaScript 运行时提供了一个 特殊绑定,允许与页面进行交互和对其进行检测。
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
try {
win.webContents.debugger.attach('1.1')
} catch (err) {
console.log('Debugger attach failed : ', err)
}
win.webContents.debugger.on('detach', (event, reason) => {
console.log('Debugger detached due to : ', reason)
})
win.webContents.debugger.on('message', (event, method, params) => {
if (method === 'Network.requestWillBeSent') {
if (params.request.url === 'https://www.github.com') {
win.webContents.debugger.detach()
}
}
})
win.webContents.debugger.sendCommand('Network.enable')
实例事件
事件:'detach'
返回
event
Eventreason
字符串 - 调试器分离的原因。
当调试会话终止时发出。这发生在 webContents
关闭或为附加的 webContents
调用 devtools 时。
事件:'message'
返回
event
Eventmethod
字符串 - 方法名。params
任意类型 - 由远程调试协议中的 'parameters' 属性定义的事件参数。sessionId
字符串 - 已附加调试会话的唯一标识符,将与debugger.sendCommand
发送的值匹配。
每当调试目标发出检测事件时发出。
实例方法
debugger.attach([protocolVersion])
protocolVersion
字符串 (可选) - 请求的调试协议版本。
将调试器附加到 webContents
。
debugger.isAttached()
返回 boolean
- 是否已将调试器附加到 webContents
。
debugger.detach()
将调试器从 webContents
分离。
debugger.sendCommand(method[, commandParams, sessionId])
method
字符串 - 方法名,应为 远程调试协议 定义的方法之一。commandParams
任意类型 (可选) - 包含请求参数的 JSON 对象。sessionId
字符串 (可选) - 将命令发送到具有关联调试会话 ID 的目标。初始值可以通过发送 Target.attachToTarget 消息获得。
返回 Promise<any>
- 一个 Promise,它将使用远程调试协议中的命令描述的 'returns' 属性进行解析,或被拒绝以指示命令失败。
将给定命令发送到调试目标。