-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path404.html
88 lines (75 loc) · 2.54 KB
/
404.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>URL Redirector</title>
<script>
/**
* Configuration
*/
const CONFIG = {
GITHUB_ISSUES_LINK: "https://api.github.com/repos/quackscience/duckdb-community-macros/issues/",
GITHUB_FILES_LINK: "https://api.github.com/repos/quackscience/duckdb-community-macros/contents/links/",
GITHUB_REPO: "https://github.com/quackscience/duckdb-community-macros",
DEBUG: true
};
function isUrl(url) {
return /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,24}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)+$/.test(url);
}
async function tryFileRedirect(path) {
try {
const response = await fetch(CONFIG.GITHUB_FILES_LINK + path);
if (!response.ok) return null;
const data = await response.json();
if (data.content && data.encoding === 'base64') {
return atob(data.content);
}
return null;
} catch (e) {
console.error('File redirect failed:', e);
return null;
}
}
async function tryIssueRedirect(issueNumber) {
try {
const response = await fetch(CONFIG.GITHUB_ISSUES_LINK + issueNumber);
if (!response.ok) return null;
const data = await response.json();
return data.title || null;
} catch (e) {
console.error('Issue redirect failed:', e);
return null;
}
}
async function redirect() {
const location = window.location;
const pathParts = location.pathname.split("/").filter(part => part);
if (pathParts.length < 2) {
location.replace(CONFIG.GITHUB_REPO);
return;
}
const [prefix, identifier] = pathParts;
let url = null;
if (prefix === 'r') {
url = await tryFileRedirect(identifier);
}
else if (prefix === 'i' && /^\d+$/.test(identifier)) {
url = await tryIssueRedirect(identifier);
}
if (url && isUrl(url.trim())) {
const targetUrl = document.createElement("a");
targetUrl.setAttribute("href", url.trim());
if (targetUrl.hostname !== location.hostname) {
location.replace(url.trim());
return;
}
}
location.replace(CONFIG.GITHUB_REPO);
}
redirect();
</script>
</head>
<body>
<noscript>JavaScript is required for redirection.</noscript>
</body>
</html>