-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
254 lines (229 loc) · 6.92 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
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
const PROTOCOL = "nonameSkill";
const {
app,
BrowserWindow,
Menu,
ipcMain,
session,
crashReporter,
} = require("electron");
const fs = require("fs");
const path = require("path");
const isWindows = process.platform === "win32";
const { versions } = process;
const electronVersion = parseFloat(versions.electron);
require("./noname-server.js");
let remote;
if (electronVersion >= 14) {
remote = require("@electron/remote/main");
remote.initialize();
} else {
// @ts-ignore
remote = require("electron").remote;
}
let win, extensionName, updateURL;
// 获取单实例锁
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
// 如果获取失败,说明已经有实例在运行了,直接退出
app.quit();
}
app.setAppUserModelId("com.shijian.noname");
//防止32位无名杀的乱码
app.setName("无名杀");
function createDir(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
}
function setPath(path1, path2) {
app.getPath(path1);
createDir(path2);
app.setPath(path1, path2);
}
setPath("home", path.join(__dirname, "Home"));
setPath("appData", path.join(__dirname, "Home", "AppData"));
setPath("userData", path.join(__dirname, "Home", "UserData"));
setPath("temp", path.join(__dirname, "Home", "Temp"));
setPath("cache", path.join(__dirname, "Home", "Cache"));
//崩溃转储文件存储的目录
setPath("crashDumps", path.join(__dirname, "Home", "crashDumps"));
//日志目录
setPath("logs", path.join(__dirname, "Home", "logs"));
//崩溃处理
crashReporter.start({
productName: "无名杀",
//崩溃报告将被收集并存储在崩溃目录中,不会上传
uploadToServer: false,
compress: false,
});
if (!app.isDefaultProtocolClient(PROTOCOL)) {
const args = [];
if (!app.isPackaged) {
// 如果是开发阶段,需要把我们的脚本的绝对路径加入参数中
args.push(path.resolve(process.argv[1]));
}
// 加一个 `--` 以确保后面的参数不被 Electron 处理
args.push("--");
app.setAsDefaultProtocolClient(PROTOCOL, process.execPath, args);
//对应的取消协议:
//app.removeAsDefaultProtocolClient(protocol)
}
function handleUrl(arr) {
const result = arr.filter((val) => val.startsWith("nonameskill:"));
if (!result.length) {
extensionName = null;
updateURL = null;
return;
}
const url = result[0];
const urlObj = new URL(url);
const { searchParams } = urlObj;
//玄武镜像:https://kuangthree.coding.net/p/noname-extensionxwjh/d/noname-extensionxwjh/git/raw/master
//链接:nonameSKill:?extensionName=全能搜索
//步骤:截取链接,跳转到另一个html里下载
extensionName = searchParams.get("extensionName");
updateURL = searchParams.get("updateURL");
}
// 如果打开协议时,没有其他实例,则当前实例当做主实例,处理参数
handleUrl(process.argv);
// 其他实例启动时,主实例会通过 second-instance 事件接收其他实例的启动参数 `argv`
app.on("second-instance", (event, argv) => {
// Windows 下通过协议URL启动时,URL会作为参数,所以需要在这个事件里处理
if (process.platform === "win32") {
handleUrl(argv);
createWindow();
}
});
// macOS 下通过协议URL启动时,主实例会通过 open-url 事件接收这个 URL
app.on("open-url", (event, urlStr) => {
handleUrl(urlStr);
createWindow();
});
app.setAboutPanelOptions({
iconPath: "noname.ico",
authors: ["诗笺"],
website: "https://github.com/nonameShijian/noname",
});
process.env["ELECTRON_DEFAULT_ERROR_MODE"] = "true";
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
process.noDeprecation = true;
function createWindow() {
let createWin;
if (extensionName) {
//暂时屏蔽通过链接下载扩展的功能
//createWin = createExtensionWindow();
createWin = createMainWindow();
} else if (updateURL) {
createWin = createUpdateWindow();
} else {
createWin = createMainWindow();
}
if (!win) {
win = createWin;
}
}
function createMainWindow() {
let win = new BrowserWindow({
width: 1000,
height: 800,
title: "无名杀",
icon: path.join(__dirname, "noname.ico"),
webPreferences: {
webSecurity: false,
preload: path.join(__dirname, "app", "menu.js"), //页面运行其他脚本之前预先加载指定的脚本
nodeIntegration: true, //主页面用node
nodeIntegrationInSubFrames: true, //子页面用node
nodeIntegrationInWorker: true, //worker用node
contextIsolation: false, //必须为false
plugins: true, //启用插件
// @ts-ignore
enableRemoteModule: true, //可以调用Remote
experimentalFeatures: true, //启用Chromium的实验功能
},
});
win.loadURL(`http://localhost:8089/app.html`);
if (electronVersion >= 14) {
remote.enable(win.webContents);
}
return win;
}
function createUpdateWindow() {
let win = new BrowserWindow({
width: 800,
height: 600,
title: "无名杀-更新文件",
icon: path.join(__dirname, "noname.ico"),
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true, //主页面用node
contextIsolation: false, //必须为false
// @ts-ignore
enableRemoteModule: true, //可以调用Remote
},
});
win.loadURL(`file://${__dirname}/update.html`);
//win.webContents.openDevTools();
win.webContents.executeJavaScript(`window.updateURL = '${updateURL}'`);
if (electronVersion >= 14) {
remote.enable(win.webContents);
}
return win;
}
app.whenReady().then(() => {
let downloadPath,
downloadExtName,
extensionWinId,
updatePath,
updateUrl,
updateWinId;
ipcMain.on("download-path", function (event, arg) {
[downloadPath, downloadExtName, extensionWinId] = arg;
event.returnValue = downloadPath;
});
ipcMain.on("update-path", function (event, arg) {
[updatePath, updateUrl, updateWinId] = arg;
event.returnValue = updatePath;
});
session.defaultSession.on("will-download", (event, item) => {
if (!updatePath || !updateUrl || !updateWinId) return;
const fileUrl = decodeURI(item.getURL()).replace(updateUrl + "/", "");
const savePath = path.join(updatePath, fileUrl);
item.setSavePath(savePath);
const winId = BrowserWindow.fromId(updateWinId);
if (!winId) return;
item.on("updated", (event, state) => {
if (winId.isDestroyed()) {
//窗口被关闭
updatePath = updateUrl = updateWinId = null;
item.cancel();
}
if (state === "interrupted") {
winId.webContents.send("update-clog", "下载被中断,但可以继续");
} else if (state === "progressing") {
if (item.isPaused()) {
winId.webContents.send("update-clog", "下载暂停");
} else {
const progress =
item.getReceivedBytes() / item.getTotalBytes();
winId.webContents.send("update-progress", progress);
}
}
});
item.once("done", (event, state) => {
if (winId.isDestroyed()) return;
winId.webContents.send("update-done", state);
});
});
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});