不推荐使用contextIsolation的默认值,并且在Electron的未来版本中将从false更改为true

我是 Electron.js 的新手。我下面一个YouTube的教程学习电子但这“contextIsolation的默认已过时,从虚假的电子的未来版本中改变为真。见https://github.com/electron/electron/issues/23506为更多信息”出现在我的控制台中。我的 main.js 文件如下:


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

const {app , BrowserWindow,Menu}=electron;

let mainWindow;

//Listen for app to be ready
app.on('ready',function() {
    //create new window
    mainWindow=new BrowserWindow({});
    //load html into window
    mainWindow.loadURL(url.format({
        pathname:path.join(__dirname,'mainWindow.html'),
        protocol:'file',
        slashes:true
    }))


    //Quit app when closed
    mainWindow.on('closed',function(){
        app.quit();
    })
    //build menu from template
    const mainMenu=Menu.buildFromTemplate(mainMenuTemplate);
    //insert menu
    Menu.setApplicationMenu(mainMenu);
});

//handle create add window
function createAddWindow(){
    //create new window
    addWindow=new BrowserWindow({
        width:300,
        height:200,
        title:'add shopping list item'
    });
     //load html into window
     addWindow.loadURL(url.format({
        pathname:path.join(__dirname,'addWndow.html'),
        protocol:'file',
        slashes:true
    }));
    //Garbage collection handle
    addWindow.on('close',function(){
        addWindow=null;
    })
}



//Create Menu template
const mainMenuTemplate=[
    {
        label:'File',
        submenu:[
            {
                label:'Add Item',
                click(){
                    createAddWindow();
                }
            },
            {
                label:'Clear Items'
            },
            {
                label:'Quit',
                accelerator:process.platform== 'darwin' ? 'command+Q' : 'ctrl+Q',
                click(){
                    app.quit();                }
            },
            
        ]
    
    }
]

回答

这只是一个弃用警告,它不会影响您的任何代码。由于您没有contextIsolation明确设置值,因此使用默认值,一旦更改,您的应用程序将不再以相同方式运行。

contextIsolation是一个可以传入webPreferencesa的选项BrowserWindow,如下所示:

const win = new BrowserWindow({
  width: 800,
  height: 600,
  webPreferences: {
    contextIsolation: true
  }
});

Electron 团队决定显示此警告,因为这contextIsolation是一个深刻改变 Electron 窗口工作方式的选项。默认情况下,Node 全局变量在执行附加到 HTML 页面的脚本之前(在执行预加载脚本之后)以编程方式从渲染器上下文中剥离,这会留下一些漏洞(例如此处演示的漏洞)。

contextIsolation确保两个上下文(预加载脚本和渲染器脚本)保持完全隔离,除了通过contextBridge. contextIsolation除非您确定不加载任何外部内容,否则建议使用,这就是默认值将被更改的原因。更多关于电子安全。


以上是不推荐使用contextIsolation的默认值,并且在Electron的未来版本中将从false更改为true的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>