简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM