在Electron中使用“ remote-debugging-port”时,如何获取Chrome的远程调试URL?

时间:2019-04-08 17:51:01

标签: javascript google-chrome electron

我在Electron主流程中为Chrome设置了remote-debugging-port选项:

app.commandLine.appendSwitch('remote-debugging-port', '8315')

现在,如何获得ws://可以用来连接Chrome的URL?

我在运行Electron时看到输出显示

DevTools listening on ws://127.0.0.1:8315/devtools/browser/52ba17be-0c0d-4db6-b6f9-a30dc10df13c

但是我想从主进程内部获取此URL。 URL每次都不同。如何从电子主过程中获取它?

我可以从我的主要流程JavaScript代码中以某种方式读取Electron的主要流程输出吗?

1 个答案:

答案 0 :(得分:0)

以下是从电子主要过程代码将Puppeteer连接到电子窗口的方法:

app.commandLine.appendSwitch('remote-debugging-port', '8315')

async function test() {
    const response = await fetch(`http://localhost:8315/json/list?t=${Math.random()}`)
    const debugEndpoints = await response.json()

    let webSocketDebuggerUrl = ''

    for (const debugEndpoint of debugEndpoints) {
        if (debugEndpoint.title === 'Saffron') {
            webSocketDebuggerUrl = debugEndpoint.webSocketDebuggerUrl
            break
        }
    }

    const browser = await puppeteer.connect({
        browserWSEndpoint: webSocketDebuggerUrl
    })

    // use puppeteer APIs now!
}

// ... make your window, etc, the usual, and then: ...

  // wait for the window to open/load, then connect Puppeteer to it:
  mainWindow.webContents.on("did-finish-load", () => { 
    test()
  })