| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { app, BrowserWindow, session } from 'electron';
- import { fileURLToPath } from 'url';
- import path from 'path';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
- // Set Content Security Policy
- function setContentSecurityPolicy() {
- session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
- const csp = isDev
- ? "default-src 'self'; script-src 'self' 'unsafe-inline' http://localhost:*; style-src 'self' 'unsafe-inline'; connect-src 'self' http://localhost:* ws://localhost:*; img-src 'self' data: https: blob:; font-src 'self' data:; worker-src 'self' blob:;"
- : "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data: https:; font-src 'self' data:;";
-
- const responseHeaders = Object.assign({}, details.responseHeaders);
- responseHeaders['Content-Security-Policy'] = [csp];
-
- callback({ responseHeaders });
- });
- }
- function createWindow() {
- const mainWindow = new BrowserWindow({
- width: 1200,
- height: 800,
- webPreferences: {
- preload: path.join(__dirname, 'preload.cjs'),
- nodeIntegration: false,
- contextIsolation: true
- }
- });
- if (isDev) {
- mainWindow.loadURL('http://localhost:5173');
- mainWindow.webContents.openDevTools();
- } else {
- mainWindow.loadFile(path.join(__dirname, 'dist/index.html'));
- }
- }
- app.whenReady().then(() => {
- setContentSecurityPolicy();
- createWindow();
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
- });
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
|