electron - 访问 Electron 5 中的节点模块
问题描述
由于 Electron 的最新版本 5 出于安全原因,nodeIntegration 默认为 false,那么推荐的访问节点模块的方式是什么?有没有办法在没有nodeIntegration的情况下与主进程通信?
解决方案
使用预加载脚本,您可以通过仅将 ipcRenderer 对象导入窗口来与主进程通信。
为此,您必须在 browserWindow webPreferences 中指定预加载脚本的绝对路径。
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
preload : path.join(__dirname , '/preload_script.js')
}
})
并在 preload_script.js 中注入 ipcRenderer 对象
window.ipcRenderer = require('electron').ipcRenderer;
您可以使用 html 脚本中的 window.ipcRenderer 对象与主进程/或另一个渲染器进程进行通信。
推荐阅读
- php - 是否有一种已知的方法来验证爱沙尼亚的企业 ID?
- node.js - 每当应用程序重新启动时,Nodejs 都会卡在处理中
- python - 捕获 SQLAlchemy 警告
- swift - 用 SwiftUI 连接 UIKit,为什么我的 NavBar 丢失了?
- react-native - TypeError: myQRCode.toDataURL 不是函数
- azure - 如何通过技能清单/端点使用 Azure Health Bot Skills?
- go - Golang:为什么我的选择不听频道?
- python - 如何确保我的 tkinter 滚动条不会向左滚动
- javascript - 输入掩码未删除
- amazon-web-services - Clouwatch运行事件规则层出不穷