首页 > 建站教程 > nodejs,electron >  electron 设置跨域可支持修改iframe正文

electron 设置跨域可支持修改iframe

Electron的主进程中禁用同源策略:

1、在Electron的主进程文件中添加app.commandLine.appendSwitch("disable-site-isolation-trials");来禁用站点隔离试验。

2、在创建BrowserWindow时,设置webPreferenceswebSecurity: 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()
}