使用"remote-debugging-port"端口时,如何获取Chrome的远程调试URL.在电子学中? [英] How can I get Chrome's remote debug URL when using the "remote-debugging-port" in Electron?

查看:941
本文介绍了使用"remote-debugging-port"端口时,如何获取Chrome的远程调试URL.在电子学中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

现在,如何获取可用于连接Chrome的ws:// URL?

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

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

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

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

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?

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

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

推荐答案

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

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

这篇关于使用"remote-debugging-port"端口时,如何获取Chrome的远程调试URL.在电子学中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆