Electron - 使用远程 url 访问已加载文档中的 DOM 元素

Electron - Access DOM elements in a loaded document using remote url

例如,我想在 window 中加载像 https://google.com 这样的远程地址,但是当单击 DOM 中的按钮时,我想在主处理器中调用一个函数。

如何在 webviewiframe 中加载的文档上添加点击侦听器以与 MainProcessor 通信?

编辑:实际上我无法直接访问文档的源代码,因为它是从远程加载的 url。

您可以使用 ipcRenderer and ipcMain 在您的进程之间传递消息。

在您的网络视图中

const ipc = require('electron').ipcMain;

document.getElementById('#myDiv').addEventListener('click', () => {
    ipc.send('click', '#myDiv');
});

在你的主进程中。

const ipc = require('electron').ipcRenderer;

ipc.on('click', (event, message) => {
    console.log(message); // logs out "#myDiv"
})

我更喜欢使用 webview,我根据下面的 link 在 webview 上设置了 preload 标签,一切都按预期工作。
Github issues page