我正在使用远程url创建一个electron BrowserWindow
,因此我不能真正使用var ipc = require('ipc');
语法来包含ipc。可以从远程url向电子主进程发送消息吗?如果是这样的话,我可以从哪里获得它的javascript源代码?
或者也许有更好的方法将信息传递给电子主进程?只需要发送登录的用户信息。
发布于 2019-06-26 08:23:07
这个问题很久以前就被问过了,但我认为这仍然是有用的。将Javascript注入浏览器窗口的方法是使用预加载延迟,在web首选项中,添加以下选项:
const window = new BrowserWindow({
webPreferences:{
preload: <path_to>/preload.js
}
})
// preload.js
const ipcRenderer = require('electron').ipcRenderer;
window.ipcRenderer = ipcRenderer
在你的网页中,你正在加载的外部url,你可以直接使用window.ipcRenderer
。
发布于 2015-11-06 13:38:09
我想我已经解决了。使用angular所以我做了一个工厂,但是相同的try/catch方法应该适用于任何事情。如果从电子运行,则发送ipc消息,否则将忽略它们。
angular.module('IpcFactory', [])
.factory('IpcFactory', function(){
var ipcAvailable;
try{
var ipc = require('ipc');
ipcAvailable = true;
}
catch(e){
ipcAvailable = false;
}
return {
ipcAvailable: ipcAvailable,
send: function(event, message){
var self = this;
if(self.ipcAvailable){
ipc.send(event, message);
}
},
on: function(event, fn){
var self = this;
if(self.ipcAvailable){
ipc.on(event, fn());
}
}
};
});
https://stackoverflow.com/questions/33537008
复制相似问题