-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (145 loc) · 3.91 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
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
const { app, BrowserWindow, dialog, shell, globalShortcut } = require("electron");
const path = require("path");
const semver = require("semver");
const https = require("https");
const url = require('url');
const utils = require("./utils");
const { session } = require("electron");
app.commandLine.appendSwitch('disable-site-isolation-trials')
function requestUpdateInfo() {
const currentVersion = app.getVersion();
return new Promise((resolve, reject) => {
https
.get(
"https://chainx-wallet-release.oss-cn-hangzhou.aliyuncs.com/latest/update.json",
res => {
let body = "";
res.on("data", d => {
body += d;
});
res.on("end", function() {
try {
const parsed = JSON.parse(body);
resolve(parsed);
} catch (err) {
reject(err);
}
});
}
)
.on("error", e => {
reject(e);
});
})
.then(updateInfo => {
if (semver.gt(updateInfo.version, currentVersion)) {
if (!updateInfo.forceUpdate) {
dialog.showMessageBox(
{
title: "更新提示",
message: `检测到有新的更新,建议下载安装更新。确认后跳转到下载页面`,
buttons: ["确认", "取消"],
defaultId: 0
},
response => {
if (response === 0) {
shell.openExternal(updateInfo.path);
}
}
);
} else {
dialog.showMessageBox(
{
title: "更新提示",
message: `检测到有新的更新,请更新到最新版本`,
buttons: ["确认"],
defaultId: 0
},
response => {
shell.openExternal(updateInfo.path);
app.quit();
}
);
}
}
})
.catch(error => {});
}
requestUpdateInfo();
app.utils = utils;
let mainWindow = null;
if (process.env.NODE_ENV === "development") {
require("electron-debug")();
}
/**
* Add event listeners...
*/
app.on("window-all-closed", () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
// app.quit();
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("before-quit", () => {
if (process.platform === "darwin") {
app.quitting = true;
}
});
app.on("activate", () => {
if (process.platform === "darwin" && mainWindow !== null) {
mainWindow && mainWindow.show();
}
});
app.on("ready", async () => {
setSession();
mainWindow = new BrowserWindow({
show: false,
width: 1320,
minWidth: 800,
minHeight: 600,
height: 765,
webPreferences: { preload: path.join(__dirname, "preload.js") },
backgroundColor: "#f2f3f4"
});
mainWindow.show();
reload(mainWindow);
mainWindow.on("close", event => {
if (process.platform === "darwin") {
if (app.quitting) {
mainWindow = null;
} else if (mainWindow !== null) {
event.preventDefault();
mainWindow.hide();
}
}
});
mainWindow.on("closed", () => {
mainWindow = null;
});
// mainWindow.webContents.on('did-fail-load', () => {
// reload(mainWindow);
// });
});
function reload(win) {
win.loadURL(
process.env.NODE_ENV === "development"
? `http://localhost:8000`
: `file://${__dirname}/app/build/index.html`
);
}
function setSession() {
// Modify the user agent for all requests to the following urls.
const filter = {
urls: ["http://127.0.0.1:21325/*"]
};
session.defaultSession.webRequest.onBeforeSendHeaders(
filter,
(details, callback) => {
details.requestHeaders["Origin"] = "http://localhost:8000";
details.requestHeaders["Referer"] = "http://localhost:8000/";
callback({ requestHeaders: details.requestHeaders });
}
);
}