首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

调试器 | Debugger

Chrome远程调试协议的备用传输方式。

过程:主

Chrome开发者工具在JavaScript运行时有一个特殊的绑定,允许与页面进行交互并对它们进行测试。

代码语言:javascript
复制
const {BrowserWindow} = require('electron')
let 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 Methods

debugger.attach([protocolVersion])

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

将调试器附加到webContents

debugger.isAttached()

返回Boolean- 调试器是否附加到webContents

debugger.detach()

从调试器中分离出来webContents

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

  • method 字符串 - 方法名称应该是远程调试协议定义的方法之一。
  • commandParams 对象(可选) - 带请求参数的JSON对象。
  • callback 功能(可选) - 响应
    • error 对象 - 指示命令失败的错误消息。
    • result 任何 - 由远程调试协议中的命令描述的“返回”属性定义的响应。

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

Instance Events

Event: ‘detach’

  • event Event
  • reason String - Reason for detaching debugger.

调试会话终止时发出。这种情况发生在webContents关闭或devtools被调用时webContents

Event: ‘message’

  • event 事件
  • method 字符串 - 方法名称。
  • params 对象 - 由远程调试协议中'参数'属性定义的事件参数。

每当调试目标问题检测仪器事件时发出。

扫码关注腾讯云开发者

领取腾讯云代金券