You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.5 KiB
119 lines
3.5 KiB
const { app, BrowserWindow } = require("electron");
|
|
const path = require("path");
|
|
const fs = require("fs");
|
|
|
|
// Check if running in dev mode
|
|
const isDev = process.argv.includes('--inspect');
|
|
|
|
function createWindow() {
|
|
// Add before createWindow function
|
|
const preloadPath = path.join(__dirname, 'preload.js');
|
|
console.log('Checking preload path:', preloadPath);
|
|
console.log('Preload exists:', fs.existsSync(preloadPath));
|
|
|
|
// Create the browser window.
|
|
const mainWindow = new BrowserWindow({
|
|
width: 1200,
|
|
height: 800,
|
|
webPreferences: {
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
webSecurity: true,
|
|
allowRunningInsecureContent: false,
|
|
preload: path.join(__dirname, 'preload.js')
|
|
},
|
|
});
|
|
|
|
// Debug info
|
|
console.log("Debug Info:");
|
|
console.log("Running in dev mode:", isDev);
|
|
console.log("App is packaged:", app.isPackaged);
|
|
console.log("Process resource path:", process.resourcesPath);
|
|
console.log("App path:", app.getAppPath());
|
|
console.log("__dirname:", __dirname);
|
|
console.log("process.cwd():", process.cwd());
|
|
|
|
const indexPath = path.join(__dirname, 'www', 'index.html');
|
|
console.log("www path:", path.join(__dirname, 'www'));
|
|
console.log("www assets path:", path.join(__dirname, 'www', 'assets'));
|
|
|
|
if (!fs.existsSync(indexPath)) {
|
|
console.error(`Index file not found at: ${indexPath}`);
|
|
throw new Error('Index file not found');
|
|
}
|
|
|
|
// Set CSP headers
|
|
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
|
|
callback({
|
|
responseHeaders: {
|
|
...details.responseHeaders,
|
|
'Content-Security-Policy': [
|
|
"default-src 'self';" +
|
|
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;" +
|
|
"font-src 'self' https://fonts.gstatic.com;" +
|
|
"script-src 'self' 'unsafe-eval' 'unsafe-inline';" +
|
|
"img-src 'self' data: https:;"
|
|
]
|
|
}
|
|
});
|
|
});
|
|
|
|
// Load the index.html
|
|
mainWindow
|
|
.loadFile(indexPath)
|
|
.then(() => {
|
|
console.log("Successfully loaded index.html");
|
|
if (isDev) {
|
|
mainWindow.webContents.openDevTools();
|
|
console.log("DevTools opened - running in dev mode");
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.error("Failed to load index.html:", err);
|
|
console.error("Attempted path:", indexPath);
|
|
});
|
|
|
|
// Listen for console messages from the renderer
|
|
mainWindow.webContents.on("console-message", (_event, level, message) => {
|
|
console.log("Renderer Console:", message);
|
|
});
|
|
|
|
// Add right after creating the BrowserWindow
|
|
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
|
|
console.error('Page failed to load:', errorCode, errorDescription);
|
|
});
|
|
|
|
mainWindow.webContents.on('preload-error', (event, preloadPath, error) => {
|
|
console.error('Preload script error:', preloadPath, error);
|
|
});
|
|
|
|
mainWindow.webContents.on('console-message', (event, level, message, line, sourceId) => {
|
|
console.log('Renderer Console:', message);
|
|
});
|
|
|
|
// Enable remote debugging when in dev mode
|
|
if (isDev) {
|
|
mainWindow.webContents.openDevTools();
|
|
}
|
|
}
|
|
|
|
// Handle app ready
|
|
app.whenReady().then(createWindow);
|
|
|
|
// Handle all windows closed
|
|
app.on("window-all-closed", () => {
|
|
if (process.platform !== "darwin") {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on("activate", () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|
|
|
|
// Handle any errors
|
|
process.on("uncaughtException", (error) => {
|
|
console.error("Uncaught Exception:", error);
|
|
});
|
|
|