Skip to main content

Class: Debugger

Class: Debugger

크롬 원격 디버깅 프로토콜을 위한 대체 전송 방식

프로세스: Main
이 클래스는 'electron' 모듈에서 직접 내보내지 않는다. Electron API의 다른 메서드의 반환값으로만 사용할 수 있다.

크롬 개발자 도구는 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 Event
  • reason string - 디버거가 분리된 이유

디버깅 세션이 종료될 때 발생한다. 이 이벤트는 webContents가 닫히거나, 연결된 webContents에 대해 개발자 도구가 호출될 때 발생한다.

이벤트: 'message'

반환값:

  • event Event - 이벤트 객체
  • method string - 메서드 이름
  • params any - 원격 디버깅 프로토콜의 'parameters' 속성에 정의된 이벤트 매개변수
  • sessionId string - 연결된 디버깅 세션의 고유 식별자. debugger.sendCommand에서 전송한 값과 일치한다.

디버깅 대상이 계측 이벤트를 발생시킬 때마다 이 이벤트가 방출된다.

인스턴스 메서드

debugger.attach([protocolVersion])

  • protocolVersion string (선택사항) - 요청된 디버깅 프로토콜 버전.

디버거를 webContents에 연결한다.

debugger.isAttached()

boolean 타입의 값을 반환한다. 이 메서드는 webContents에 디버거가 연결되어 있는지 여부를 확인한다.

debugger.detach()

webContents에서 디버거를 분리한다.

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

  • method string - 메서드 이름. 원격 디버깅 프로토콜에 정의된 메서드 중 하나여야 한다.
  • commandParams any (선택 사항) - 요청 파라미터를 담은 JSON 객체.
  • sessionId string (선택 사항) - 디버깅 세션 ID와 연관된 대상에 명령을 전송한다. 초기 값은 Target.attachToTarget 메시지를 전송해 얻을 수 있다.

Promise<any>를 반환한다. 이 Promise는 원격 디버깅 프로토콜의 명령 설명에 있는 'returns' 속성에 정의된 응답으로 해결되거나, 명령 실패 시 거부된다.

디버깅 대상에 주어진 명령을 전송한다.