[英]How do I use node.js in index.html without nodeIntegration [electron]
我開始構建一個新的 electron 應用程序,但它需要在 html 頁面中使用某些 node.js 函數。 我知道設置nodeIntegration: true
是不安全的,而且我知道我可以以某種方式使用preload.js
,但我不知道如何。 任何和所有的幫助將不勝感激。 謝謝!
PS是否有任何情況下nodeIntegration: true
是安全的,否則他們為什么會有它?
EDIT: Just to clarify, I want my HTML in data/screens/index.html
, and my index.html
's javascript in data/screens/js/index.js
, and, if possible, to still be able to use things例如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);
};
或者
嘗試使用 electron 的遠程模塊( https://electronjs.org/docs/api/remote )
或者
訪問主進程中的模塊,調用預期結果的函數並使用 IPC 將結果發送回渲染器進程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.