forked from SpiritSeal/redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path404.html
82 lines (73 loc) · 2.47 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Redirecting...</title>
<script>
/**
* Configuration goes here
*/
// Format: https://api.github.com/repos/{owner}/{repo}/issues/
var GITHUB_ISSUES_LINK =
"https://api.github.com/repos/greyhatgt/redirect/issues/";
var PATH_SEGMENTS_TO_SKIP = 1;
// List of allowed GitHub usernames (will only redirect for issues by these users)
var ALLOWED_USERS = ["SpiritSeal"];
/**
* DO NOT TOUCH ANYTHING BELOW THIS COMMENT UNLESS YOU KNOW WHAT YOU ARE DOING
*/
function isNumeric(num){ return !isNaN(num); }
function isUrl(url) {
return /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,24}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)+$/.test(url);
}
var isQuery = window.location.href.substr(-1) == "?";
var keyword = window.location.pathname.split("/")[PATH_SEGMENTS_TO_SKIP + 1];
var xhr = new XMLHttpRequest();
xhr.onload = function () {
var payload = JSON.parse(xhr.response);
var issue = "";
// keyword is issue number
if (isNumeric(keyword)) {
issue = payload;
// keyword is shortened url name
} else {
for (var i=0; i<payload.length; i++) {
if (payload[i].body == keyword) {
// stop searching after first issue that matches
issue = payload[i];
break;
}
}
}
// if something was returned through GH
if (issue != "") {
// check if author is in allowed users list
if (ALLOWED_USERS.includes(issue.user.login)) {
var url = issue.title;
} else {
var url = "Failed authentication (GitHub Issue not created by an authorized user)";
}
} else {
// no issue matched
var url = "Not found";
}
// update page
document.getElementById("link").textContent = url;
// if current url ends in '?', just display the full url
if (isUrl(url) && !isQuery) {
// redirect user
window.location.replace(url);
}
}
// Send XHR request
var api_url = GITHUB_ISSUES_LINK;
if (isNumeric(keyword)) { api_url += keyword; }
else { api_url = api_url.slice(0, -1); }
xhr.open("GET", api_url);
xhr.send();
</script>
</head>
<body>
<h1 id="link">404</h1>
</body>
</html>