How can I get Chrome's remote debug URL when using the “remote-debugging-port” in Electron?

倖福魔咒の 提交于 2019-12-13 05:06:52

问题


I've set the remote-debugging-port option for Chrome in my Electron main process:

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

Now, how can I get the ws:// URL that I can use to connect to Chrome?

I see that the output while I'm running Electron shows

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

but I would like to get this URL from inside the main process. The URL is different every time. How can I get it from inside the Electron main process?

Can I somehow read my Electron's main process output, from within my main process JavaScript code?


回答1:


Here's how to connect Puppeteer to your Electron window from your Electron main process code:

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()
  })


来源:https://stackoverflow.com/questions/55579075/how-can-i-get-chromes-remote-debug-url-when-using-the-remote-debugging-port-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!