forked from libccy/noname
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·371 lines (342 loc) · 11.7 KB
/
index.html
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="user-scalable=no, viewport-fit=cover" />
<title>无名杀</title>
<script>
if (
typeof window.require == "function" &&
typeof window.process == "object" &&
typeof window.__dirname == "string"
) {
// 使importMap解析node内置模块
const builtinModules = require("module").builtinModules;
if (Array.isArray(builtinModules)) {
const importMap = {
imports: {},
};
for (const module of builtinModules) {
importMap.imports[module] = importMap.imports[`node:${module}`] =
`./noname-builtinModules/${module}`;
}
const im = document.createElement("script");
im.type = "importmap";
im.textContent = JSON.stringify(importMap);
document.currentScript.after(im);
}
}
</script>
<script>
"use strict";
(() => {
window.onerror = function (msg, src, line, column, err) {
let str = `错误文件: ${typeof src == "string" && src.length > 0 ? decodeURI(src) : "未知文件"}`;
str += `\n错误信息: ${msg}`;
str += `\n行号: ${line}`;
str += `\n列号: ${column}`;
if (err && err.stack) str += "\n" + decodeURI(err.stack);
console.error(str);
alert(str);
};
const STACK_REGEXP = /^\s*at .*(\S+:\d+|\(native\))/m;
const errorList = [];
function extractLocation(urlLike) {
// 不存在地址信息的字符串
if (!/:/.test(urlLike)) {
return [urlLike];
}
// 捕获位置用到的正则
const regExp = /(.+?)(?::(\d+))?(?::(\d+))?$/;
const parts = regExp.exec(urlLike.replace(/[()]/g, ""));
// @ts-ignore
return [parts[1], parts[2] || void 0, parts[3] || void 0];
}
window.onunhandledrejection = async (event) => {
event.promise.catch((error) => {
console.log(error);
// 如果`error`是个错误,则继续处理
if (error instanceof Error) {
// 如果已经处理过该错误,则不再处理
if (errorList.includes(error)) return;
errorList.push(error);
// 如果`error`拥有字符串形式的报错栈堆,且报错栈堆确实符合v8的stack
if (typeof error.stack === "string" && STACK_REGEXP.test(error.stack)) {
// 获取符合栈堆信息的字符串,一般来说就是从第二行开始的所有行
// 为了处理eval的情况,故必须获取完行数
let lines = error.stack.split("\n").filter((line) => STACK_REGEXP.test(line));
// 提供类型信息防止vscode报错
/**
* @type {string | undefined}
*/
let fileName = void 0;
/**
* @type {number | undefined}
*/
let line = void 0;
/**
* @type {number | undefined}
*/
let column = void 0;
// 从第一条开始遍历,一直遍历到不存在eval的位置
for (let currentLine = 0; currentLine < lines.length; ++currentLine) {
if (/\(eval /.test(lines[currentLine])) continue;
let formatedLine = lines[currentLine]
.replace(/^\s+/, "")
.replace(/\(eval code/g, "(")
.replace(/^.*?\s+/, "");
const location = formatedLine.match(/ (\(.+\)$)/);
if (location) formatedLine = formatedLine.replace(location[0], "");
const locationParts = extractLocation(location ? location[1] : formatedLine);
fileName = ["eval", "<anonymous>"].includes(locationParts[0])
? void 0
: locationParts[0];
line = Number(locationParts[1]);
column = Number(locationParts[2]);
break;
}
// @ts-ignore
window.onerror(error.message, fileName, line, column, error);
}
// 反之我们只能不考虑报错文件信息,直接调用onerror
else {
try {
// @ts-ignore
let [_, src = void 0, line = void 0, column = void 0] =
/at\s+.*\s+\((.*):(\d*):(\d*)\)/i.exec(error.stack.split("\n")[1]);
if (typeof line == "string") line = Number(line);
if (typeof column == "string") column = Number(column);
// @ts-ignore
window.onerror(error.message, src, line, column, error);
} catch (e) {
window.onerror(error.message, "", 0, 0, error);
}
}
}
});
};
})();
</script>
<script>
"use strict";
if (
location.href.startsWith("http") &&
typeof window.initReadWriteFunction != "function" &&
!window.require &&
!window.__dirname
) {
window.initReadWriteFunction = async function (game) {
return new Promise((resolve, reject) => {
fetch(`./checkFile?fileName=noname.js`)
.then((response) => {
return response.json();
})
.then((result) => {
if (result && result.success) callback();
else reject(result.errorMsg);
})
.catch(reject);
function callback() {
/**
* 检查指定的路径是否是一个文件
*
* @param {string} fileName - 需要查询的路径
* @param {(result: -1 | 0 | 1) => void} [callback] - 回调函数;接受的参数意义如下:
* - `-1`: 路径不存在或无法访问
* - `0`: 路径的内容不是文件
* - `1`: 路径的内容是文件
* @param {(err: Error) => void} [onerror] - 接收错误的回调函数
* @return {void} - 由于三端的异步需求和历史原因,文件管理必须为回调异步函数
*/
game.checkFile = function (fileName, callback, onerror) {
fetch(`./checkFile?fileName=${fileName}`)
.then(response => response.json())
.then(result => {
if (result) {
if (result.success) {
callback?.(1);
return;
}
if (result.code === 404) {
if (result.errorMsg === "不是一个文件") return void callback?.(0);
else if (result.errorMsg === "文件不存在或无法访问") return void callback?.(-1);
}
}
onerror?.(result?.errorMsg);
})
.catch(onerror);
}
/**
* 检查指定的路径是否是一个目录
*
* @param {string} dir - 需要查询的路径
* @param {(result: -1 | 0 | 1) => void} [callback] - 回调函数;接受的参数意义如下:
* - `-1`: 路径不存在或无法访问
* - `0`: 路径的内容不是目录
* - `1`: 路径的内容是目录
* @param {(err: Error) => void} [onerror] - 接收错误的回调函数
* @return {void} - 由于三端的异步需求和历史原因,文件管理必须为回调异步函数
*/
game.checkDir = function (dir, callback, onerror) {
fetch(`./checkDir?dir=${dir}`)
.then(response => response.json())
.then(result => {
if (result) {
if (result.success) {
callback?.(1);
return;
}
if (result.code === 404) {
if (result.errorMsg === "不是一个文件夹") return void callback?.(0);
else if (result.errorMsg === "文件夹不存在或无法访问") return void callback?.(-1);
}
}
onerror?.(result?.errorMsg);
})
.catch(onerror);
}
game.readFile = function (fileName, callback = () => {}, error = () => {}) {
fetch(`./readFile?fileName=${fileName}`)
.then((response) => {
return response.json();
})
.then((result) => {
if (result && result.success)
callback(new Uint8Array(result.data).buffer);
else error(result && result.errorMsg);
})
.catch(error);
};
game.readFileAsText = function (fileName, callback = () => {}, error = () => {}) {
fetch(`./readFileAsText?fileName=${fileName}`)
.then((response) => {
return response.json();
})
.then((result) => {
if (result && result.success) callback(result.data);
else error(result && result.errorMsg);
})
.catch(error);
};
game.writeFile = function (data, path, name, callback = () => {}) {
game.ensureDirectory(path, function () {
if (Object.prototype.toString.call(data) == "[object File]") {
const fileReader = new FileReader();
fileReader.onload = function (e) {
game.writeFile(e.target.result, path, name, callback);
};
fileReader.readAsArrayBuffer(data, "UTF-8");
} else {
let filePath = path;
if (path.endsWith("/")) {
filePath += name;
} else if (path == "") {
filePath += name;
} else {
filePath += "/" + name;
}
fetch(`./writeFile`, {
method: "post",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data:
typeof data == "string"
? data
: Array.prototype.slice.call(new Uint8Array(data)),
path: filePath,
}),
})
.then((response) => {
return response.json();
})
.then((result) => {
if (result && result.success) {
callback();
} else {
callback(result.errorMsg);
}
});
}
});
};
game.removeFile = function (fileName, callback = () => {}) {
fetch(`./removeFile?fileName=${fileName}`)
.then((response) => {
return response.json();
})
.then((result) => {
callback(result.errorMsg);
})
.catch(error);
};
game.getFileList = function (dir, callback = () => {}, onerror) {
fetch(`./getFileList?dir=${dir}`)
.then((response) => {
return response.json();
})
.then((result) => {
if (!result) {
throw new Error("Cannot get available resource.");
}
if (result.success) {
callback(result.data.folders, result.data.files);
} else if (onerror) {
onerror(new Error(result.errorMsg));
}
});
};
game.ensureDirectory = function (list, callback = () => {}, file = false) {
let pathArray = typeof list == "string" ? list.split("/") : list;
if (file) {
pathArray = pathArray.slice(0, -1);
}
game.createDir(pathArray.join("/"), callback, console.error);
};
game.createDir = (
directory,
successCallback = () => {},
errorCallback = () => {}
) => {
fetch(`./createDir?dir=${directory}`)
.then((response) => {
return response.json();
})
.then((result) => {
if (result && result.success) {
successCallback();
} else {
errorCallback(new Error("创建文件夹失败"));
}
})
.catch(errorCallback);
};
game.removeDir = (
directory,
successCallback = () => {},
errorCallback = () => {}
) => {
fetch(`./removeDir?dir=${directory}`)
.then((response) => {
return response.json();
})
.then((result) => {
if (result && result.success) {
successCallback();
} else {
errorCallback(new Error("创建文件夹失败"));
}
})
.catch(errorCallback);
};
resolve();
}
});
};
}
</script>
<script src="game/update.js"></script>
<script src="game/config.js"></script>
<script src="game/package.js"></script>
<script src="game/game.js"></script>
</head>