Skip to main content

Class: Debugger

Class: Debugger

An alternate transport for Chrome's remote debugging protocol.Chrome远程调试协议的替代传输。

Process:进程:Main
This class is not exported from the 'electron' module. 此类不是从'electron'模块导出的。It is only available as a return value of other methods in the Electron API.它只能作为Electron API中其他方法的返回值使用。

Chrome Developer Tools has a special binding available at JavaScript runtime that allows interacting with pages and instrumenting them.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')

Instance Events实例事件

Event: 'detach'

Returns:返回:

  • event Event
  • reason string - Reason for detaching debugger.分离调试器的原因。

Emitted when the debugging session is terminated. 在调试会话终止时发出。This happens either when webContents is closed or devtools is invoked for the attached webContents.当关闭webContents或为附加的webContents调用devtools时,就会发生这种情况。

Event: 'message'

Returns:返回:

  • event Event
  • method string - Method name.方法名称。
  • params any - Event parameters defined by the 'parameters' attribute in the remote debugging protocol.由远程调试协议中的“parameters”属性定义的事件参数。
  • sessionId string - Unique identifier of attached debugging session, will match the value sent from debugger.sendCommand.附加调试会话的唯一标识符,将与从debugger.sendCommand发送的值相匹配。

Emitted whenever the debugging target issues an instrumentation event.每当调试目标发出检测事件时发出。

Instance Methods实例方法

debugger.attach([protocolVersion])

  • protocolVersion string (optional) - Requested debugging protocol version.请求的调试协议版本。

Attaches the debugger to the webContents.将调试器附加到webContents

debugger.isAttached()

Returns返回boolean - Whether a debugger is attached to the webContents.是否将调试器附加到webContents

debugger.detach()

Detaches the debugger from the webContents.webContents中分离调试器。

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

  • method string - Method name, should be one of the methods defined by the remote debugging protocol.方法名称,应该是由远程调试协议定义的方法之一
  • commandParams any (optional) - JSON object with request parameters.带有请求参数的JSON对象。
  • sessionId string (optional) - send command to the target with associated debugging session id. 将命令发送到具有关联调试会话id的目标。The initial value can be obtained by sending Target.attachToTarget message.初始值可以通过发送Target.attachToTarget消息来获得。

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.使用远程调试协议中命令描述的“return”属性定义的响应解析的promise,或者被拒绝,表示命令失败。

Send given command to the debugging target.将给定的命令发送到调试目标。