/
index.js
92 lines (78 loc) 路 1.83 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
import { parse as parseUrl } from 'url';
import app from 'app';
import BrowserWindow from 'browser-window';
import menu from './menu';
const HOST = `localhost:${process.env.PORT || 19998}`;
const DEV = process.env.ENV==='development';
// adds debug features like hotkeys for triggering dev tools and reload
if (DEV) {
try { require('electron-debug')(); }catch(err){}
}
// prevent window being garbage collected
let mainWindow;
app.on('ready', () => {
mainWindow = createMainWindow();
overrideWindowOpen();
});
function createMainWindow() {
const win = new BrowserWindow({
width: DEV ? 1200 : 800,
height: DEV ? 600 : 500,
minWidth: 500,
minHeight: 200,
'accept-first-mouse': true,
'title-bar-style': 'hidden'
});
menu(win);
win.on('closed', () => {
mainWindow = null;
});
if (DEV) {
win.loadURL(`http://${HOST}/`);
win.toggleDevTools();
}
else {
win.loadURL(`file://${__dirname}/../index.html`);
}
return win;
}
function overrideWindowOpen() {
mainWindow.webContents.on('new-window', (e, url, name, disp, options) => {
let host = parseUrl(url).host;
if (host && host!==HOST) {
Object.assign(options, {
width: 400,
height: 500,
center: true,
frame: true,
resizable: false,
title: `Loading ${host}...`,
'title-bar-style': 'visible',
'always-on-top': true,
'use-content-size': true,
'skip-taskbar': true,
'node-integration': false,
'web-preferences': {
'web-security': true
}
});
}
else {
console.warn(`Allowing node integration for URL: ${url}`);
}
});
}
// quit if all windows are closed
app.on('window-all-closed', () => app.quit() );
/*
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate-with-no-open-windows', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
*/