简体   繁体   English

有没有办法让 Electron 主进程脚本监听带有 `postMessage` function 的页面发布的消息?

[英]Is there a way for a Electron main process script to listen on messages posted by a page with `postMessage` function?

I want to use the standard postMessage API function in my page scripts that are loaded by an Electron renderer process.我想在由 Electron 渲染器进程加载的页面脚本中使用标准postMessage API function。

I want to stick to Web APIs in renderer process scripts because the application is designed to run in both Electron and on the Web (in user's preferred Web browser).我想在渲染器进程脚本中坚持使用 Web API,因为该应用程序设计为在 Electron 和 Web(在用户首选的 Web 浏览器中)上运行。 I don't want to tax source code readers with the likes of if(typeof electron.= "undefined")... or similar.我不想用if(typeof electron.= "undefined")...或类似的东西来征税源代码读者。

Is there a way to listen on messages that are generated by the postMessage function, or events dispatched by a script window, in an Electron main process script?有没有办法在 Electron 主进程脚本中监听由postMessage function 生成的消息,或由脚本 window 调度的事件? That will allow me to maintain a graceful fallback in the renderer process script -- a posted message that isn't processed won't also generate runtime errors if not running in Electron.这将允许我在渲染器进程脚本中保持优雅的回退——如果未在 Electron 中运行,则未处理的已发布消息也不会生成运行时错误。

So far I have been using the preload property of the webPreferences object being passed to BrowserWindow constructor to load the following preload script which sets up a "communication bridge" I am after:到目前为止,我一直在使用传递给BrowserWindow构造函数的webPreferences object 的preload属性来加载以下预加载脚本,该脚本设置了我所追求的“通信桥”:

(() => {
    const electron = require("electron");
    addEventListener("message", ev => {
        switch(ev.data.type) {
            case "foobar":
                electron.ipcRenderer.send("foobar", ev.data.foobar); break;
        }
    });
})();

I can then do a postMessage({ type: "foobar", foobar: { foo: 1, bar: true } }, "*") as part of any renderer process script, and it will basically result in the renderer process sending the message to the main process, with the above set up by the preload script.然后我可以做一个postMessage({ type: "foobar", foobar: { foo: 1, bar: true } }, "*")作为任何渲染器进程脚本的一部分,它基本上会导致渲染器进程发送消息到主进程,上面由预加载脚本设置。

Ideally, though, if possible, I'd like to avoid having to use a preload script, but like I said I don't want to use any Electron APIs in renderer process scripts at all.理想情况下,如果可能的话,我想避免使用预加载脚本,但正如我所说,我根本不想在渲染器进程脚本中使用任何 Electron API。 If I could listen to messages posted by the renderer script "window" or to events dispatched by it, directly in the main process, that would eliminate my need for the preload script.如果我可以直接在主进程中收听渲染器脚本“window”发布的消息或它分派的事件,那将消除我对预加载脚本的需要。 But I don't know if it is possible, and reading the documentation gives me the impression I am going in circles or that I have exhausted my alternatives.但我不知道这是否可能,阅读文档给我的印象是我在兜圈子,或者我已经用尽了我的替代方案。

Not directly, but you can preload a script on the browser window that receives that message on the renderer process and copy that with ipcRenderer.send or invoke .不是直接的,但您可以在浏览器 window 上预加载一个脚本,该脚本在渲染器进程上接收该消息,并使用ipcRenderer.sendinvoke复制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从主进程启动的电子通道消息未被渲染器进程接收 - Electron channel messages initiated from the main process not received by the renderer process 如何在 Angular-Electron 中监听从主进程到渲染器进程的事件 - How to listen for an event from the main process to the renderer process in Angular-Electron 电子主流程上的jQuery - jQuery on electron main process 有什么方法可以向电子中的每个渲染器进程发送异步消息? - is there any way to send async messages to every renderer process in electron? 电子:有什么方法可以通过ipc访问/修改主进程数组 - Electron: Is there any way to access/modify a main process array over ipc 我们如何将消息从主进程发送到 Electron 中的渲染器进程 - How can we send messages from the main process to renderer process in Electron 从主进程创建不可见窗口以计算Electron中的函数 - Creating invisible window from main process to compute function in Electron Electron Builder:“主进程中发生错误 - 回调不是函数” - Electron Builder: "Error occurred in the main process - Callback is not a function" 有没有办法在同一个 iframe 上向 postMessage 添加多条消息? - Is there a way to add multiple messages to postMessage on the same iframe? IPC消息阻止了Electron中的进程 - IPC messages are blocking the process in Electron
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM