跳至主要內容

類別:偵錯器

類別:偵錯器

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

執行個體事件

事件:'detach'

傳回

  • event 事件
  • reason 字串 - 中斷偵錯器的原因。

當偵錯階段終止時發出。當 webContents 關閉或為已附加的 webContents 呼叫開發人員工具時,會發生這種情況。

事件:'message'

傳回

  • event 事件
  • method 字串 - 方法名稱。
  • params any - 遠端除錯協定中「參數」屬性定義的事件參數。
  • sessionId 字串 - 已附加偵錯階段的唯一識別碼,將與從 debugger.sendCommand 送出的值相符。

每當偵錯目標發出檢測事件時發出。

執行個體方法

debugger.attach([protocolVersion])

  • protocolVersion 字串(可選) - 請求的除錯協定版本。

將偵錯器附加到 webContents

debugger.isAttached()

傳回 boolean - 偵錯器是否已附加到 webContents

debugger.detach()

webContents 中斷偵錯器。

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

  • method 字串 - 方法名稱,應為遠端除錯協定定義的方法之一。
  • commandParams any(可選) - 包含請求參數的 JSON 物件。
  • sessionId 字串(可選) - 將命令傳送至具有相關偵錯階段 ID 的目標。初始值可以透過傳送 Target.attachToTarget 訊息取得。

傳回 Promise<any> - 承諾會使用遠端除錯協定中命令描述的「傳回」屬性所定義的回應解析,或是被拒絕以表示命令失敗。

將指定命令傳送至偵錯目標。