在Electron
的主进程中禁用同源策略:
1、在Electron
的主进程文件中添加app.commandLine.appendSwitch("disable-site-isolation-trials");
来禁用站点隔离试验。
2、在创建BrowserWindow
时,设置webPreferences
的webSecurity: false
来禁用同源策略。
具体代码:
const { app, BrowserWindow } = require('electron') const path = require('node:path') // 禁用站点隔离试验 app.commandLine.appendSwitch("disable-site-isolation-trials"); const createWindow = () => { // Create the browser window. const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { // 禁用同源策略 webSecurity: false, preload: path.join(__dirname, 'preload.js') } }) // 加载 index.html mainWindow.loadFile('index.html') // 打开开发工具 mainWindow.webContents.openDevTools() }