类:Debugger
类:Debugger
Chrome 远程调试协议的备用传输方式。
进程:主进程
此类未从 'electron'
模块导出。 它仅作为 Electron API 中其他方法的返回值提供。
Chrome 开发者工具在 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
事件reason
string - 分离调试器的原因。
当调试会话终止时发出。这发生在 webContents
关闭或为附加的 webContents
调用 devtools 时。
事件:'message'
返回
event
事件method
string - 方法名称。params
any - 由远程调试协议中 'parameters' 属性定义的事件参数。sessionId
string - 附加调试会话的唯一标识符,将匹配从debugger.sendCommand
发送的值。
每当调试目标发出检测事件时发出。
实例方法
debugger.attach([protocolVersion])
protocolVersion
string (可选) - 请求的调试协议版本。
将调试器附加到 webContents
。
debugger.isAttached()
返回 boolean
- 调试器是否附加到 webContents
。
debugger.detach()
将调试器从 webContents
分离。
debugger.sendCommand(method[, commandParams, sessionId])
method
string - 方法名称,应为远程调试协议定义的方法之一。commandParams
any (可选) - 包含请求参数的 JSON 对象。sessionId
string (可选) - 将命令发送到具有关联调试会话 id 的目标。初始值可以通过发送 Target.attachToTarget 消息获取。
返回 Promise<any>
- 一个 Promise,它会使用远程调试协议中命令描述的 'returns' 属性定义的响应解决,或者在命令失败时被拒绝。
将给定的命令发送到调试目标。