Skip to main content

类:Debugger

类:Debugger

🌐 Class: Debugger

Chrome 远程调试协议的替代传输方式。

进程: 主进程
此类未从 'electron' 模块导出。它仅作为 Electron API 中其他方法的返回值提供。

Chrome 开发者工具在 JavaScript 运行时提供了 特殊装订,可以与页面进行交互并进行操作。

🌐 Chrome Developer Tools has a special binding available at JavaScript runtime that allows interacting with pages and instrumenting them.

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

实例事件

🌐 Instance Events

Event: 'detach'

返回:

🌐 Returns:

  • event 活动
  • reason 字符串 - 断开调试器的原因。

当调试会话终止时触发。这种情况发生在关闭 webContents 或为附加的 webContents 调用开发者工具时。

🌐 Emitted when the debugging session is terminated. This happens either when webContents is closed or DevTools is invoked for the attached webContents.

Event: 'message'

返回:

🌐 Returns:

  • event 活动
  • method 字符串 - 方法名称。
  • params 任意 - 由远程调试协议中的 'parameters' 属性定义的事件参数。
  • sessionId 字符串 - 附加调试会话的唯一标识符,将与 debugger.sendCommand 发送的值匹配。

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

🌐 Emitted whenever the debugging target issues an instrumentation event.

实例方法

🌐 Instance Methods

debugger.attach([protocolVersion])

  • protocolVersion 字符串(可选)- 请求的调试协议版本。

将调试器附加到 webContents

🌐 Attaches the debugger to the webContents.

debugger.isAttached()

返回 boolean - 指示是否有调试器附加到 webContents

🌐 Returns boolean - Whether a debugger is attached to the webContents.

debugger.detach()

将调试器从 webContents 分离。

🌐 Detaches the debugger from the webContents.

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

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

返回 Promise<any> - 一个 Promise,它会解析为远程调试协议中命令描述的 'returns' 属性所定义的响应,或者在命令失败时被拒绝。

🌐 Returns Promise<any> - A promise that resolves with the response defined by the 'returns' attribute of the command description in the remote debugging protocol or is rejected indicating the failure of the command.

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

🌐 Send given command to the debugging target.