gpt4 book ai didi

javascript - 如何在没有nodeIntegration的情况下在index.html中使用node.js [ Electron ]

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:27 24 4
gpt4 key购买 nike

我开始构建一个新的 Electron 应用程序,但它需要在 html 页面中使用某些 node.js 函数。我知道设置 nodeIntegration: true 并不安全,而且我知道我可以以某种方式使用 preload.js,但我不知道如何使用。任何和所有的帮助将不胜感激。谢谢!

附注是否有任何情况下 nodeIntegration: true 是安全的,否则他们为什么要拥有它?

编辑:为了澄清一下,我希望我的 HTML 位于 data/screens/index.html 中,而我的 index.html 的 javascript 位于 data/中screen/js/index.js,并且如果可能的话,仍然能够使用诸如 document.innerHTML 之类的东西。我不知道这是否可能,但如果我不问,我永远不会知道。 :)

最佳答案

您可以在创建主浏览器窗口时使用 webPreferences 中的预加载参数。在main.js中,

  mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
}
})

在preload.js中

   window.notify= function notify(msg) {
return require('node-notifier').notify(msg);
};

或者

尝试使用 Electron 的远程模块( https://electronjs.org/docs/api/remote )

或者

访问主进程中的模块,调用预期结果的函数,并使用 IPC 将结果发送回渲染进程。

关于javascript - 如何在没有nodeIntegration的情况下在index.html中使用node.js [ Electron ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com