[英]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.send
或invoke
复制它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.