-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
85 lines (74 loc) · 2.31 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
const { app, BrowserWindow, protocol } = require("electron");
const contextMenu = require('electron-context-menu');
const path = require("path");
const basepath = app.getAppPath();
const fixPath = require('fix-path');
// const isDev = require("electron-is-dev");
const server = require("./modules/server");
let mainWindow;
fixPath();
const isDev = process.env.MERCURY_ENV === "development";
const resourcePath = isDev ? basepath : process.resourcesPath;
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = true;
contextMenu({
showCopyImage: true,
showCopyImageAddress: true,
showSaveImageAs: true,
showServices: true,
showInspectElement: true,
prepend: (defaultActions, params, browserWindow) => [
{
label: 'Force Reload',
click: () => {
browserWindow.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(resourcePath, "build", "index.html")}`)
}
}
]
});
async function createWindow() {
mainWindow = new BrowserWindow({
width: 900,
height: 680,
webPreferences: {
title: "Mercury",
nodeIntegration: false,
preload: __dirname + `/preload.js`,
webSecurity: false,
icon: path.join(__dirname, 'assets/icons/png/icon_128x128.png')
}
});
mainWindow.setMenu(null)
protocol.interceptFileProtocol(
"file",
(request, callback) => {
const Url = new URL(request.url);
const pathname = Url.pathname[0] === "/" ? Url.pathname.substr(1, Url.pathname.length) : Url.pathname;
let servedUrl = pathname;
let token = pathname.split("MERCURY/WEBAPP/PATH");
if (pathname.match("MERCURY/WEBAPP/PATH")) {
servedUrl = `${path.join(resourcePath, "build", token[token.length - 1] || "")}`;
}
callback(servedUrl);
},
err => {
if (err) console.error("Failed to register protocol");
}
);
mainWindow.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(resourcePath, "build", "index.html")}`);
if (isDev) {
// mainWindow.webContents.openDevTools();
}
mainWindow.on("closed", () => (mainWindow = null));
mainWindow.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
}
app.on("ready", server.initializeServer);
app.on("ready", createWindow);
app.on("activate", () => {
if (mainWindow === null) {
createWindow();
}
});