我正在为自己的目的创建一个电子应用程序。我的问题是,当我在我的HTML页面内使用节点函数时,它抛出了一个错误:

'require()'没有定义。

是否有办法在所有HTML页面中使用Node功能?如果有可能,请给我一个如何做到这一点的例子或提供一个链接。下面是我试图在我的HTML页面中使用的变量:

  var app = require('electron').remote; 
  var dialog = app.dialog;
  var fs = require('fs');

这些是我在电子中所有HTML窗口中使用的值。


当前回答

我所要做的只是在我的html页面中要求一个js文件,因为我正在遵循的教程。但是,我打算使用远程模块,所以安全性是最重要的。我在上面修改了迈克尔的答案,所以我发布了出来,纯粹是为了那些像我一样花了几个小时寻找安全替代“要求”的人。如果代码不正确,请随时指出来。

main.js

const electron = require('electron');
const app=electron.app;
const BrowserWindow=electron.BrowserWindow;
const ipcMain=electron.ipcMain;

const path=require('path');
const url=require('url');

let win;

function createWindow(){
    win=new BrowserWindow({
        webPreferences:{
            contextIsolation: true,
            preload: path.join(__dirname, "preload.js")
        }
    });
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file',
        slashes: true
    }));

    win.on('close', function(){
        win=null
    });
}

app.on('ready', createWindow);

preload.js

const electron=require('electron');
const contextBridge=electron.contextBridge;

contextBridge.exposeInMainWorld(
    "api", {
        loadscript(filename){
            require(filename);
        }
    }
);

index . html

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World App</title>
    </head>
    <body>
        <h1>Hello World</h1>
        <button id="btn">Click</button>
    </body>
    <script>
        window.api.loadscript('./index.js');
    </script>
</html>

index.js

const btn = document.getElementById('btn');
btn.addEventListener('click', function(){
    console.log('button clicked');
});

我特别想知道这是否仍然存在安全风险。谢谢。

其他回答

我所要做的只是在我的html页面中要求一个js文件,因为我正在遵循的教程。但是,我打算使用远程模块,所以安全性是最重要的。我在上面修改了迈克尔的答案,所以我发布了出来,纯粹是为了那些像我一样花了几个小时寻找安全替代“要求”的人。如果代码不正确,请随时指出来。

main.js

const electron = require('electron');
const app=electron.app;
const BrowserWindow=electron.BrowserWindow;
const ipcMain=electron.ipcMain;

const path=require('path');
const url=require('url');

let win;

function createWindow(){
    win=new BrowserWindow({
        webPreferences:{
            contextIsolation: true,
            preload: path.join(__dirname, "preload.js")
        }
    });
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file',
        slashes: true
    }));

    win.on('close', function(){
        win=null
    });
}

app.on('ready', createWindow);

preload.js

const electron=require('electron');
const contextBridge=electron.contextBridge;

contextBridge.exposeInMainWorld(
    "api", {
        loadscript(filename){
            require(filename);
        }
    }
);

index . html

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World App</title>
    </head>
    <body>
        <h1>Hello World</h1>
        <button id="btn">Click</button>
    </body>
    <script>
        window.api.loadscript('./index.js');
    </script>
</html>

index.js

const btn = document.getElementById('btn');
btn.addEventListener('click', function(){
    console.log('button clicked');
});

我特别想知道这是否仍然存在安全风险。谢谢。

如果你只是不关心任何安全问题,并且想要在浏览器窗口上被JavaScript正确地解释,那么在main.js代码中有一个额外的标志:

webPreferences: { nodeIntegration:没错, nodeIntegrationInWorker:没错, nodeIntegrationInSubFrames:没错, enableRemoteModule:没错, contextIsolation: false //required标志 } //其余的代码…

从版本5开始,nodeIntegration的默认值从true变为false。 您可以在创建浏览器窗口时启用它:

app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });
});

最后,我成功了。将此代码添加到HTML文档Script Element中。

很抱歉回复晚了。我使用下面的代码来做这件事。

window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;

使用nodeRequire而不是require。

很好。

看起来Electron的安全性是这样进化的(来源)。

Electron 1 nodeIntegration默认为true

Renderer可以完全访问Node API—如果Renderer加载远程代码,则存在巨大的安全风险。

Electron 5 nodeIntegration默认为false

当设置为false时,将使用预加载脚本向Renderer公开特定的API。(不管nodeIntegration的值如何,预加载脚本总是可以访问Node api)

//preload.js
window.api = {
    deleteFile: f => require('fs').unlink(f)
}

Electron 5 contextIsolation默认为true(实际上在Electron 11中仍然默认为false)

这将导致预加载脚本在单独的上下文中运行。你不能再做窗户了。API = ....你现在要做的是:

//preload.js
const { contextBridge } = require('electron')

contextBridge.exposeInMainWorld('api', {
    deleteFile: f => require('fs').unlink(f)
})

电子6 require()节点内置沙盒渲染器不再隐式加载远程版本

如果Renderer有沙盒设置为true,你必须做:

//preload.js
const { contextBridge, remote } = require('electron')

contextBridge.exposeInMainWorld('api', {
    deleteFile: f => remote.require('fs').unlink(f)
})

Electron 10 enableRemoteModule默认为false(远程模块在Electron 12中已弃用)

remote模块用于当你需要从沙盒渲染器中访问Node api时(如上面的例子);或者当你需要访问仅对主进程可用的电子api时(如对话框,菜单)。如果没有remote,您需要编写如下所示的显式IPC处理程序。

//preload.js
const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('api', {
    displayMessage: text => ipcRenderer.invoke("displayMessage", text)
})

//main.js
const { ipcMain, dialog } = require('electron')

ipcMain.handle("displayMessage", text => dialog.showMessageBox(text))

Electron 10弃用节点集成标志(在Electron 12中删除)

建议

总是设置{nodeIntegration: false, contextIsolation: true, enableRemoteModule: false}。

为了最大的安全性,设置{sandbox: true}。您的预加载脚本将必须使用IPC调用主进程来完成所有工作。

如果sandbox为false,你的预加载脚本可以直接访问Node API,如require('fs'). readfile。你是安全的,只要你不这样做

//bad
contextBridge.exposeInMainWorld('api', {
    readFile: require('fs').readFile
})