-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
98 lines (86 loc) · 2.82 KB
/
main.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
const { app, BrowserWindow, globalShortcut, dialog, screen } = require('electron');
const path = require('path');
const fs = require('fs');
const setupRequestInterceptor = require('./interceptor');
let mainWindow;
const COMMAND_LINE_ARGS = {
NO_FULLSCREEN: '--no-fullscreen',
DEFAULT_CURSOR: '--default-cursor',
DISABLE_CSS: '--disable-css',
NO_HIDE_CURSOR: '--no-hide-cursor'
};
function parseCommandLineArgs() {
return {
noFullscreen: process.argv.includes(COMMAND_LINE_ARGS.NO_FULLSCREEN),
defaultCursor: process.argv.includes(COMMAND_LINE_ARGS.DEFAULT_CURSOR),
disableCSS: process.argv.includes(COMMAND_LINE_ARGS.DISABLE_CSS),
noHideCursor: process.argv.includes(COMMAND_LINE_ARGS.NO_HIDE_CURSOR)
};
}
async function createWindow({ noFullscreen, defaultCursor, disableCSS, noHideCursor }) {
const { width, height } = screen.getPrimaryDisplay().size;
mainWindow = new BrowserWindow({
width: width,
height: height,
show: false,
fullscreen: false,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js'),
backgroundThrottling: false
}
});
mainWindow.setMenu(null);
mainWindow.loadURL('https://pokerogue.net/');
mainWindow.webContents.on('did-finish-load', async () => {
const cssPath = path.join(__dirname, 'styles.css');
const css = fs.readFileSync(cssPath, 'utf-8');
if (!disableCSS) {
mainWindow.webContents.insertCSS(css);
}
await setupRequestInterceptor(mainWindow);
mainWindow.webContents.send('setup-mouse-move-handler', defaultCursor, noHideCursor);
mainWindow.setFullScreen(!noFullscreen);
mainWindow.show();
});
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('setup-mouse-move-handler', defaultCursor, noHideCursor);
});
mainWindow.webContents.on('did-fail-load', () => {
dialog.showErrorBox('Network Error', 'Unable to connect to Pokérogue servers.');
app.quit();
});
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.on('focus', () => {
globalShortcut.register('CommandOrControl+Q', () => {
app.quit();
});
mainWindow.webContents.on('before-input-event', (event, input) => {
if (input.type === 'keyDown') {
if (input.key === 'F11') {
event.preventDefault();
mainWindow.setFullScreen(!mainWindow.isFullScreen());
} else if (input.key === 'F5') {
event.preventDefault();
mainWindow.reload();
}
}
});
});
}
if (!app.requestSingleInstanceLock()) {
app.quit();
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
app.whenReady().then(() => {
const cmdArgs = parseCommandLineArgs();
createWindow(cmdArgs);
});
}