-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
122 lines (102 loc) · 3.22 KB
/
main.js
File metadata and controls
122 lines (102 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import path from "path";
import { fileURLToPath } from "url";
import { app, BrowserWindow, ipcMain, powerSaveBlocker } from "electron";
import fs from "fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log("Main process started.");
console.log("Current directory:", __dirname);
console.log("File URL:", __filename);
const preloadPath = path.join(__dirname, "preload.js");
console.log("Preload script path:", preloadPath);
let mainWindow;
let timer = null;
let blockerId = null;
function createWindow() {
const preloadPath = path.join(__dirname, "preload.js");
const indexPath = path.join(__dirname, "index.html");
// Check if preload.js exists
if (!fs.existsSync(preloadPath)) {
console.error(`Preload script not found at: ${preloadPath}`);
return;
}
// Check if index.html exists
if (!fs.existsSync(indexPath)) {
console.error(`HTML file not found at: ${indexPath}`);
return;
}
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
contextIsolation: true, // Ensure context isolation is enabled
enableRemoteModule: false, // Disable remote module for security
preload: preloadPath, // Ensure the preload path is correct
},
});
mainWindow.loadFile(indexPath);
mainWindow.on("closed", () => {
mainWindow = null;
});
console.log("Main window created successfully.");
}
function setCSP() {
const csp = `
default-src 'self';
script-src 'self';
style-src 'self' 'unsafe-inline';
img-src 'self' data:;
connect-src 'self';
font-src 'self';
`;
const indexPath = path.join(__dirname, "index.html");
let htmlContent = fs.readFileSync(indexPath, "utf8");
// Inject CSP meta tag into the <head> of the HTML
if (!htmlContent.includes("Content-Security-Policy")) {
htmlContent = htmlContent.replace(
"<head>",
`<head><meta http-equiv="Content-Security-Policy" content="${csp}">`
);
fs.writeFileSync(indexPath, htmlContent, "utf8");
console.log("CSP added to index.html");
}
}
function startTimer(event, intervalMs) {
clearInterval(timer); // Clear any existing timer
timer = setInterval(() => {
event.sender.send("tick");
}, intervalMs);
}
function stopTimer() {
clearInterval(timer);
}
app
.whenReady()
.then(() => {
console.log("App is ready.");
setCSP(); // Inject CSP before creating the window
createWindow();
// Prevent the system from throttling the app
blockerId = powerSaveBlocker.start("prevent-app-suspension");
// Handle app activation (e.g., when clicking the dock icon on macOS)
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
})
.catch(err => {
console.error("Error during app initialization:", err);
});
app.on("window-all-closed", () => {
if (blockerId !== null) {
powerSaveBlocker.stop(blockerId);
}
if (process.platform !== "darwin") app.quit();
});
// --- IPC logic for timer ---
ipcMain.on("start-timer", (event, intervalMs) => startTimer(event, intervalMs));
ipcMain.on("stop-timer", stopTimer);
process.on("uncaughtException", error => {
console.error("Uncaught exception in main process:", error);
});