-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (82 loc) · 3.18 KB
/
Copy pathindex.js
File metadata and controls
82 lines (82 loc) · 3.18 KB
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
export function commandResultProcesser(command, ...moreInfo) {
alert(`命令: ${command}\n你可以直接粘贴(Ctrl+V)命令到MC中执行`);
navigator.clipboard.writeText(command);
console.log(command);
for (const info of moreInfo) {
console.log(info);
}
}
export function registerRouter(options) {
hashes = [...new Set(options.filter((item) => typeof item === 'string').concat(options.filter((item) => Array.isArray(item)).map((item) => item[0])))].concat(hashes);
hashHandlers = hashHandlers.concat(options.filter((item) => Array.isArray(item)));
if (!registeredFirstRouter) {
window.addEventListener('hashchange', hashChangeHandler);
registeredFirstRouter = true;
}
hashChangeHandler();
}
export function getFetchOptions(mime) {
return {
headers: {
accept: `${mime}; charset=utf-8`,
'cache-control': 'no-cache'
},
cache: 'no-cache'
};
}
let hashes = [];
let hashHandlers = [];
let registeredFirstRouter = false;
function hashChangeHandler() {
const hash = location.hash.slice(2);
if (hashes.includes(hash)) {
fetch(hash + '.html', getFetchOptions('text/html'))
.then(response => {
if (!response.ok) {
console.groupCollapsed('网络报错信息');
console.log('状态:', response.statusText);
console.log('请求URL:', response.url);
console.groupEnd();
alert('网络错误! 请重试\n可在控制台查看详情');
}
return response.text();
})
.then(text => {
if (text.startsWith('<!')) {
const firstNewlineIndex = text.indexOf('\n');
text = firstNewlineIndex !== -1 ? text.slice(firstNewlineIndex + 1) : text;
}
const doc = new DOMParser().parseFromString(text, 'text/html');
document.body.replaceWith(doc.body);
doc.querySelectorAll('script').forEach(script => {
if (script.type === 'importmap') {
throw new TypeError('不支持importmap');
}
if (script.src) {
if (script.type === 'module') {
import(script.src).then(m => {
const def = m.default;
if (def) {
window[def.name ?? 'summon'] = def;
}
});
return;
}
fetch(script.src, getFetchOptions('text/javascript'))
.then(async (response) => new Function(await response.text())());
}
else {
new Function(script.textContent ?? '')();
}
});
});
}
const indexOfHashOfHashHandlers = hashes.indexOf(hash);
if (Array.isArray(indexOfHashOfHashHandlers)) {
const handler = indexOfHashOfHashHandlers[1];
if (typeof handler === 'function') {
handler(location);
}
}
}
;