How can I get Chrome's remote debug URL when u

2019-08-22 00:22发布

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条回答
叛逆
2楼-- · 2019-08-22 00:46

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()
  })
查看更多
登录 后发表回答