原因electron 4.x开始后的安全策略,不识别file:///开始的url
解决方案:main.js中,加入webSecurity: false,并且自定义file:///协议的解析
import { app, BrowserWindow, ipcMain, protocol } from 'electron'
function createWindow () {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
height: 768,
useContentSize: true,
width: 1300,
webPreferences:{
webSecurity: false, //========关闭安全策略===========
nodeIntegration: true,
}
})
//===========自定义file:///协议的解析=======================
protocol.interceptFileProtocol('file', (req, callback) => {
const url = req.url.substr(8);
callback(decodeURI(url));
}, (error) => {
if (error) {
console.error('Failed to register protocol');
}
});
}
app.on('ready', createWindow)
在app ready后,注册file:///协议,自己识别该协议