跳转到主要内容

类:Debugger

Class: 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')

实例事件

Event: 'detach'

返回

  • event Event
  • reason string - 分离调试器的原因。

当调试会话终止时发出。这会在 webContents 关闭或为已附加的 webContents 调用 devtools 时发生。

事件:'message'

返回

  • event Event
  • method string - 方法名。
  • params any - 由远程调试协议中的 'parameters' 属性定义的事件参数。
  • sessionId string - 已附加调试会话的唯一标识符,将匹配从 debugger.sendCommand 发送的值。

每当调试目标发出检测事件时发出。

实例方法

debugger.attach([protocolVersion])

  • protocolVersion string (optional) - 请求的调试协议版本。

将调试器附加到 webContents

debugger.isAttached()

Returns boolean - 是否已将调试器附加到 webContents

debugger.detach()

将调试器从 webContents 分离。

debugger.sendCommand(method[, commandParams, sessionId])

  • method string - 方法名,应为远程调试协议定义的方法之一。
  • commandParams any (optional) - 包含请求参数的 JSON 对象。
  • sessionId string (optional) - 向具有关联调试会话 ID 的目标发送命令。初始值可以通过发送 Target.attachToTarget 消息获得。

Returns Promise<any> - 一个 Promise,它会以远程调试协议中命令描述的 'returns' 属性定义的值解析,或者被拒绝指示命令失败。

将给定命令发送到调试目标。