[英]Electron ipc using remote website?
I'm creating a electron BrowserWindow
using a remote url, so I can't really use the var ipc = require('ipc');
我正在使用远程URL创建电子 BrowserWindow
,所以我不能真正使用var ipc = require('ipc');
syntax to include ipc. 包含ipc的语法。 It is possible to send messages from a remote url to the electron main-process? 是否可以从远程URL向电子主进程发送消息? If so where can I get the javascript source for it? 如果是这样,我在哪里可以获取它的javascript源?
Or maybe there is a better way to pass info to electron main-process? 也许有更好的方法将信息传递给电子主过程? Just need to send the logged in user info. 只需发送已登录的用户信息。
This question has been asked quite sometime back, but I think this could still be useful. 这个问题已经有一段时间问了,但我认为这可能仍然有用。 The way you can inject Javascript into the browser window is using the preload lag, in the web preferences, add the options: 将Javascript注入浏览器窗口的方式是使用预加载滞后,在Web首选项中,添加以下选项:
const window = new BrowserWindow({
webPreferences:{
preload: <path_to>/preload.js
}
})
// preload.js
const ipcRenderer = require('electron').ipcRenderer;
window.ipcRenderer = ipcRenderer
In the webpage of you the external url you are loading you can directly use window.ipcRenderer
. 在您正在加载的外部URL的网页中,可以直接使用window.ipcRenderer
。
I believe I solved it. 我相信我解决了。 Using angular so I made a factory, but the same try/catch method should work for anything. 使用angular可以创建工厂,但是相同的try / catch方法应该适用于任何情况。 If run from electron the ipc messages are sent, otherwise they are ignored. 如果从电子运行,则发送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());
}
}
};
});
If found that setting the nodeIntegration to true as part of the webPreferences fixed this issue and allowed "require" the ipc module and call a send on it 如果发现作为webPreferences的一部分将nodeIntegration设置为true可以解决此问题,并允许“需要” ipc模块并对其调用发送
main.js
var authWindow = new BrowserWindow({
width: 450,
height: 300,
show: false,
parent: mainWindow,
modal: true,
webPreferences:{
nodeIntegration: true
}
});
Please be aware of the security implications of setting this is you are possibly executing scripts from unknown origins 请注意设置此项的安全隐患,因为您可能正在执行未知来源的脚本
https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.