This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (170 loc) · 4.72 KB
/
index.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use strict";
// Initialize unhandled exceptions handler first
const { is, openNewGitHubIssue, debugInfo } = require("electron-util");
const unhandled = require("electron-unhandled");
unhandled({
reportButton: (error) => {
openNewGitHubIssue({
user: "Lovinity",
repo: "wwsu-timesheets",
body: `
<!-- Below, please describe what you were doing leading up to the issue (steps to reproduce) -->
<!-- Below, please explain what you expected to happen -->
<!-- Below, please explain what actually happened, including relevant error messages -->
---
The following is auto-generated information about the error you received.
${error.message}
${error.stack}
---
The following is auto-generated information about the app version you are using and the OS you are running.
${debugInfo()}`,
});
},
});
const path = require("path");
const {
app,
BrowserWindow,
Menu,
ipcMain,
session,
shell,
} = require("electron");
const debug = require("electron-debug");
const contextMenu = require("electron-context-menu");
const config = require("./config");
const menu = require("./menu");
const packageJson = require("./package.json");
const { machineIdSync } = require("./assets/wwsu-host-id-timesheets");
const semver = require("semver");
debug();
contextMenu();
app.setAppUserModelId(packageJson.appId);
// Prevent window from being garbage collected
let mainWindow;
const enforceCORS = () => {
// Enforce CORS and Origin
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
...details.responseHeaders,
"Content-Security-Policy": !is.development
? [`script-src 'self' https://server.wwsu1069.org`]
: [],
Origin: "file://",
},
});
});
};
const createMainWindow = async () => {
const win = new BrowserWindow({
title: app.name,
show: false,
width: 600,
height: 400,
fullscreen: true,
autoHideMenuBar: true, // Do not show manu bar unless alt is pressed
webPreferences: {
contextIsolation: true,
enableRemoteModule: false, // electron's remote module is insecure
preload: path.join(__dirname, "preload-renderer.js"),
zoomFactor: 1.25,
},
});
win.on("ready-to-show", () => {
win.show();
});
win.on("closed", () => {
// Dereference the window
// For multiple windows store them in an array
mainWindow = undefined;
});
await win.loadFile(path.join(__dirname, "renderer.html"));
return win;
};
// Prevent multiple instances of the app
if (!app.requestSingleInstanceLock()) {
app.quit();
}
app.on("second-instance", () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
app.on("window-all-closed", () => {
app.quit();
});
app.on("activate", () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
(async () => {
await app.whenReady();
Menu.setApplicationMenu(menu);
enforceCORS();
mainWindow = await createMainWindow();
})();
/*
IPC COMMUNICATIONS
*/
// Sync get the machine ID string for this installation
ipcMain.on("get-machine-id", (event) => {
event.returnValue = machineIdSync();
});
// Sync Get the app and version info
ipcMain.on("get-app-version", (event) => {
event.returnValue = `${packageJson.name} v${packageJson.version}`;
});
// Sync check if a version is newer than our version
ipcMain.on("check-version", (event, arg) => {
if (semver.gt(arg, packageJson.version)) {
event.returnValue = { current: packageJson.version };
}
event.returnValue = false;
});
// Sync return settings store
ipcMain.on("settings", (event, arg) => {
event.returnValue = config.get(arg);
});
// Save new settings
ipcMain.on("saveSettings", (event, arg) => {
config.set(arg[0], arg[1]);
});
// Tasks to be completed by the main process
ipcMain.on("main", (event, arg) => {
var args = arg[1];
switch (arg[0]) {
// Generate a notification window
case "makeNotification":
((data) => {
let notificationWindow = new BrowserWindow({
width: 640,
height: 480,
show: false,
center: true,
alwaysOnTop: true,
minimizable: false,
maximizable: false,
autoHideMenuBar: true, // Do not show manu bar unless alt is pressed
title: `WWSU Timesheets - ${data.title}`,
webPreferences: {
contextIsolation: true,
enableRemoteModule: false, // electron's remote module is insecure
preload: path.join(__dirname, "preload-notification.js"),
backgroundThrottling: false, // Do not throttle this process.
zoomFactor: 1.25,
},
});
notificationWindow.once("ready-to-show", () => {
notificationWindow.show();
notificationWindow.webContents.send("notificationData", data);
});
notificationWindow.loadFile("notification.html");
})(arg[1][0]);
break;
}
});