forked from nelsontky/gh-pages-url-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
404.html
77 lines (68 loc) · 2.28 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
<!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/mindflow-ai/short-url/issues/";
var PATH_SEGMENTS_TO_SKIP = 0;
/**
* DO NOT TOUCH ANYTHING BELOW THIS COMMENT UNLESS YOU KNOW WHAT YOU ARE DOING
*/
function isUrl(url) {
// Regex from https://stackoverflow.com/a/3809435, with a modification to allow for TLDs of up to 24 characters
return /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,24}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)+$/.test(
url
);
}
function redirect() {
var location = window.location;
var issueNumber = location.pathname.split("/")[
PATH_SEGMENTS_TO_SKIP + 1
];
var homepage =
location.protocol +
"//" +
location.hostname +
(location.port ? ":" + location.port : "") +
"/" +
location.pathname.split("/")[PATH_SEGMENTS_TO_SKIP];
var xhr = new XMLHttpRequest();
xhr.onload = function () {
try {
var payload = JSON.parse(xhr.response);
var message = payload.message;
var title = payload.title;
// Workaround IE 11 lack of support for new URL()
var url = document.createElement("a");
url.setAttribute("href", title);
// Invalid URLs includes invalid issue numbers, issue titles that are not URLs, and recursive destination URLs
var isInvalidUrl =
message === "Not Found" ||
!title ||
!isUrl(title) ||
url.hostname === location.hostname;
if (isInvalidUrl) {
location.replace(homepage);
} else {
location.replace(title);
}
} catch (e) {
location.replace(homepage);
}
};
xhr.onerror = function () {
location.replace(homepage);
};
xhr.open("GET", GITHUB_ISSUES_LINK + issueNumber);
xhr.send();
}
redirect();
</script>
</head>
<body></body>
</html>