Skip to main content

类:Debugger

类:Debugger

¥Class: Debugger

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

¥An alternate transport for Chrome's remote debugging protocol.

进程:主进程
该类不是从 'electron' 模块导出的。它仅可用作 Electron API 中其他方法的返回值。

¥Process: Main
This class is not exported from the 'electron' module. It is only available as a return value of other methods in the 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

事件:'detach'

¥Event: 'detach'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • reason 字符串 - 分离调试器的原因。

    ¥reason string - Reason for detaching debugger.

调试会话终止时发出。当 webContents 关闭或为附加的 webContents 调用 devtools 时,会发生这种情况。

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

事件:'message'

¥Event: 'message'

返回:

¥Returns:

  • event 事件

    ¥event Event

  • method 字符串 - 方法名称。

    ¥method string - Method name.

  • params 任意 - 远程调试协议中 '参数' 属性定义的事件参数。

    ¥params any - Event parameters defined by the 'parameters' attribute in the remote debugging protocol.

  • sessionId 字符串 - 附加调试会话的唯一标识符,将与从 debugger.sendCommand 发送的值匹配。

    ¥sessionId string - Unique identifier of attached debugging session, will match the value sent from debugger.sendCommand.

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

¥Emitted whenever the debugging target issues an instrumentation event.

实例方法

¥Instance Methods

debugger.attach([protocolVersion])

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

    ¥protocolVersion string (optional) - Requested debugging protocol version.

将调试器连接到 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 字符串 - 方法名称,应该是 远程调试协议 定义的方法之一。

    ¥method string - Method name, should be one of the methods defined by the remote debugging protocol.

  • commandParams 任意(可选) - 带有请求参数的 JSON 对象。

    ¥commandParams any (optional) - JSON object with request parameters.

  • sessionId 字符串(可选) - 将命令发送到具有关联调试会话 ID 的目标。初始值可以通过发送 Target.attachToTarget 消息获取。

    ¥sessionId string (optional) - send command to the target with associated debugging session id. The initial value can be obtained by sending Target.attachToTarget message.

返回 Promise<any> - 使用远程调试协议中命令描述的 'returns' 属性定义的响应进行解析的 promise,或者被拒绝指示命令失败。

¥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.