最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

我如何在没有nodeIntegration [electron]的index.html中使用node.js

IT培训 admin 5浏览 0评论

我如何在没有nodeIntegration [electron]的index.html中使用node.js

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

P.S。在任何情况下nodeIntegration: true is是安全的,因为否则他们为什么会拥有它?

编辑:为了澄清,我希望我的HTML在data/screens/index.html中,而我的index.html的Javascript在data/screens/js/index.js中,并且,如果可能的话,仍然能够使用例如document.innerHTML之类的东西。我不知道是否有可能,但是我不问我是否会问。 :)

回答如下:

创建主BrowserWindow时,可以在webPreferences中使用preload参数。在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);
   };

OR

尝试使用电子远程模块(https://electronjs/docs/api/remote)

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

我如何在没有nodeIntegration [electron]的index.html中使用node.js

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

P.S。在任何情况下nodeIntegration: true is是安全的,因为否则他们为什么会拥有它?

编辑:为了澄清,我希望我的HTML在data/screens/index.html中,而我的index.html的Javascript在data/screens/js/index.js中,并且,如果可能的话,仍然能够使用例如document.innerHTML之类的东西。我不知道是否有可能,但是我不问我是否会问。 :)

回答如下:

创建主BrowserWindow时,可以在webPreferences中使用preload参数。在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);
   };

OR

尝试使用电子远程模块(https://electronjs/docs/api/remote)

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

发布评论

评论列表 (0)

  1. 暂无评论