-
Notifications
You must be signed in to change notification settings - Fork 22
/
setup.js
297 lines (263 loc) · 7.97 KB
/
setup.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const os = require("os");
const app_config = {
debug: false,
size: {
default: {
width: 1366,
height: 768,
},
r2k: {
width: 1920,
height: 1080,
},
r4k: {
width: 2845,
height: 1600,
},
},
useSize: null,
minWidth: 1280,
minHeight: 720,
icon: `${__dirname}/../doc/logo.ico`,
main: `file://${__dirname}/index.html`,
log_configure: null,
};
if ("darwin" == os.platform().toLowerCase()) {
app_config.icon = `${__dirname}/../doc/logo.png`; // darwin should use png as icon
}
const electron = require("electron");
// Module to control application life.
const { app } = electron;
// Module to create native browser window.
const { BrowserWindow } = electron;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win = null;
let hold = false;
const custom_selectors = {
files: [],
selectors: [],
};
const INPUT_PARAMS_MODE = {
NONE: 0,
INPUT_FILE: 1,
CUSTOM_BUTTON: 2,
LOG_CONFIGURE: 3,
};
function readCustomSelectors(file_path) {
const fs = require("fs");
return new Promise((resolve, reject) => {
fs.readFile(
file_path,
{
encoding: "utf8",
flag: "r",
},
(err, data) => {
if (err) {
reject(`Read file ${file_path} failed: ${err.toString()}`);
return;
}
try {
const ret = [];
const jsonContent = JSON.parse(data);
if (Array.isArray(jsonContent)) {
for (var i = 0; i < jsonContent.length; ++i) {
ret.push(jsonContent[i]);
}
} else {
ret.push(jsonContent);
}
resolve(ret);
} catch (e) {
reject(`Parse json of ${file_path} failed: ${e.toString()}`);
}
}
);
});
}
async function readAllCustomSelectors(custom_selector_files) {
const ret = [];
for (const file_path of custom_selector_files) {
try {
const result = await readCustomSelectors(file_path);
if (Array.isArray(result)) {
for (var i = 0; i < result.length; ++i) {
ret.push(result[i]);
}
} else {
ret.push(result);
}
} catch (err) {
ret.push(err);
}
}
return ret;
}
function createWindow() {
const electron = require("electron");
const { screen } = electron;
// Create a window that fills the screen's available work area.
const primaryDisplay = screen.getPrimaryDisplay();
const screen_width = primaryDisplay.workAreaSize.width;
const screen_height = primaryDisplay.workAreaSize.height;
if (screen_width >= 3600 && screen_height >= 1920) {
app_config.useSize = app_config.size.r4k;
} else if (screen_width >= 2440 && screen_height >= 1280) {
app_config.useSize = app_config.size.r2k;
} else {
app_config.useSize = app_config.size.default;
}
console.log(
`Initialize screen size: ${screen_width}x${screen_height}, window size: ${app_config.useSize.width}x${app_config.useSize.height}`
);
var main_url = app_config.main;
if (process) {
var input_file = null;
custom_selectors.files = [];
var param_mode = INPUT_PARAMS_MODE.NONE;
for (const v of process.argv) {
switch (param_mode) {
case INPUT_PARAMS_MODE.INPUT_FILE: {
input_file = v;
param_mode = INPUT_PARAMS_MODE.NONE;
break;
}
case INPUT_PARAMS_MODE.CUSTOM_BUTTON: {
custom_selectors.files.push(v);
param_mode = INPUT_PARAMS_MODE.NONE;
break;
}
case INPUT_PARAMS_MODE.LOG_CONFIGURE: {
app_config.log_configure = v;
param_mode = INPUT_PARAMS_MODE.NONE;
break;
}
default: {
if (v == "--input") {
param_mode = INPUT_PARAMS_MODE.INPUT_FILE;
} else if (v == "--custom-selector" || v == "--custom-button") {
param_mode = INPUT_PARAMS_MODE.CUSTOM_BUTTON;
} else if (v == "--debug-mode") {
app_config.debug = true;
} else if (v == "--log-configure") {
param_mode = INPUT_PARAMS_MODE.LOG_CONFIGURE;
}
break;
}
}
}
if (input_file) {
main_url =
main_url +
"?input=" +
encodeURIComponent(input_file.replace(/\\/g, "/"));
}
}
// Create the browser window.
const current_win = new BrowserWindow({
width: app_config.useSize.width,
height: app_config.useSize.height + (app_config.debug ? 28 : 0),
minWidth: app_config.minWidth,
minHeight: app_config.minHeight + (app_config.debug ? 28 : 0),
resizable: app_config.debug,
movable: true,
closable: true,
fullscreenable: app_config.debug,
skipTaskbar: false,
frame: true,
autoHideMenuBar: !app_config.debug,
icon: app_config.icon,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
nodeIntegrationInSubFrames: true,
contextIsolation: false,
},
});
hold = false;
// Emitted when the window is closed.
current_win.on("closed", () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
if (win == current_win) {
win = null;
}
});
// and load the index.html of the app.
current_win.loadURL(main_url);
// Open the DevTools.
if (app_config.debug) {
current_win.webContents.openDevTools();
}
win = current_win;
}
const { ipcMain } = require("electron");
ipcMain.on("ipc-main", (event, arg) => {
if (arg === "reload") {
setTimeout(function () {
hold = true;
if (win) {
win.close();
}
createWindow();
}, 50);
}
event.reply("ok");
});
ipcMain.on("ipc-resize-window", (event, arg) => {
if (win) {
//win.setSize(arg.width, arg.height + app_config.useSize.height + (app_config.debug ? 28 : 0));
win.setContentSize(
Math.min(Math.ceil(arg.width), 1778),
Math.min(Math.ceil(arg.height), 1000)
);
}
event.reply("ok");
});
ipcMain.handle("ipc-get-custom-selectors", async (event, _) => {
if (custom_selectors.selectors && custom_selectors.selectors.length > 0) {
return custom_selectors.selectors;
}
if (!custom_selectors.files || custom_selectors.files.length <= 0) {
return custom_selectors.selectors;
}
return await readAllCustomSelectors(custom_selectors.files).then((ret) => {
custom_selectors.selectors = ret;
return ret;
});
});
ipcMain.handle("ipc-reload-custom-selectors", (event, _) => {
custom_selectors.selectors = [];
});
ipcMain.handle("ipc-get-log4js", async (event, _) => {
return app_config.log_configure;
});
ipcMain.handle("ipc-get-app-version", async (event, _) => {
const appVersion = app.getVersion();
return appVersion;
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (!hold && process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (!hold && win === null) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.