forked from nelsontky/gh-pages-url-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path404.html
More file actions
215 lines (185 loc) · 6.8 KB
/
Copy path404.html
File metadata and controls
215 lines (185 loc) · 6.8 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Redirecting...</title>
<style>
/* Only used in "display" mode, when the issue content isn't a link */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
max-width: 700px;
margin: 3rem auto;
padding: 0 1rem;
color: #1a1a1a;
line-height: 1.5;
white-space: pre-wrap;
display: none; /* revealed only if we end up rendering content */
}
</style>
<script>
/**
* ============================
* CONFIGURATION
* ============================
*/
var CONFIG = {
// owner/repo whose issues back this redirector
repo: "dsewerek/dsewerek",
// how many path segments to skip before the "slug" part of the URL
// e.g. for https://dsewerek.github.io/<skip>/WHATEVER_I_WANT -> 1
// any remaining segments are rejoined with "/" as the slug, so
// /go/foo/bar also works as a key of "foo/bar"
pathSegmentsToSkip: 0,
// field on the issue used as the lookup KEY (the slug you type in the URL)
keyField: "title",
// field on the issue used as the VALUE (the redirect target / content)
valueField: "body",
// match slug against keyField case-insensitively
caseSensitive: false,
// "redirect": only ever try to redirect; if invalid, fall back to homepage
// "display" : never redirect; always show the raw content on the page
// "auto" : redirect if the content looks like a valid link target,
// otherwise just display the content as-is
mode: "redirect",
// URL schemes that are considered valid redirect targets when found
// as an absolute URL (only relevant in "redirect"/"auto" modes)
allowedSchemes: ["http:", "https:", "mailto:", "tel:", "ftp:"],
// allow relative / path-only targets, e.g. "/docs/setup" or "../foo.html"
allowRelativePaths: true,
// block redirecting back to the exact same host (avoids obvious loops)
preventSameHostRedirect: true,
// issues are listed via the API (state=all, up to 100 per page); bump
// this if you have more than a few hundred entries and need more pages
maxPagesToScan: 3,
};
/**
* ============================
* DO NOT TOUCH BELOW THIS LINE
* UNLESS YOU KNOW WHAT YOU ARE DOING
* ============================
*/
var GITHUB_ISSUES_LIST =
"https://api.github.com/repos/" +
CONFIG.repo +
"/issues?state=all&per_page=100";
function parseUrl(candidate) {
var a = document.createElement("a");
a.setAttribute("href", candidate);
return a;
}
// Absolute URL with an allowed scheme, e.g. https://example.com/foo
function isAbsoluteAllowedUrl(str) {
var re = /^([a-zA-Z][a-zA-Z0-9+.-]*):/; // scheme:...
var match = re.exec(str);
if (!match) return false;
var scheme = match[1].toLowerCase() + ":";
return CONFIG.allowedSchemes.indexOf(scheme) !== -1;
}
// Bare relative/path-only target: "/foo", "foo/bar", "../baz", "?q=1", "#frag"
function isRelativePath(str) {
if (!CONFIG.allowRelativePaths) return false;
if (/\s/.test(str)) return false; // whitespace -> treat as plain content, not a path
if (str.length > 2048) return false; // sanity cap
return /^([./#?]|[a-zA-Z0-9_-]+\/)/.test(str);
}
function isRedirectTarget(str) {
if (!str) return false;
str = str.trim();
if (isAbsoluteAllowedUrl(str)) return true;
if (isRelativePath(str)) return true;
return false;
}
function wouldLoop(target) {
if (!CONFIG.preventSameHostRedirect) return false;
if (!isAbsoluteAllowedUrl(target)) return false; // relative paths can't loop by host
var url = parseUrl(target);
return url.hostname === window.location.hostname;
}
function showContent(text) {
document.body.textContent = text;
document.body.style.display = "block";
}
function normalizeKey(str) {
str = (str || "").trim();
return CONFIG.caseSensitive ? str : str.toLowerCase();
}
function handleMatch(issue, homepage) {
var content = issue[CONFIG.valueField];
if (CONFIG.mode === "display") {
showContent(content || "");
return;
}
var canRedirect =
isRedirectTarget(content) && !wouldLoop(content.trim());
if (CONFIG.mode === "redirect") {
window.location.replace(canRedirect ? content.trim() : homepage);
return;
}
// mode === "auto"
if (canRedirect) {
window.location.replace(content.trim());
} else {
showContent(content || "");
}
}
function fetchPage(pageNum, slugKey, homepage, onDone) {
if (pageNum > CONFIG.maxPagesToScan) {
onDone(null);
return;
}
var xhr = new XMLHttpRequest();
xhr.onload = function () {
try {
var issues = JSON.parse(xhr.response);
if (!Array.isArray(issues)) {
onDone(null); // e.g. rate-limited / error payload
return;
}
for (var i = 0; i < issues.length; i++) {
if (normalizeKey(issues[i][CONFIG.keyField]) === slugKey) {
onDone(issues[i]);
return;
}
}
if (issues.length < 100) {
onDone(null); // last page, no match
} else {
fetchPage(pageNum + 1, slugKey, homepage, onDone);
}
} catch (e) {
onDone(null);
}
};
xhr.onerror = function () {
onDone(null);
};
xhr.open("GET", GITHUB_ISSUES_LIST + "&page=" + pageNum);
xhr.send();
}
function redirect() {
var location = window.location;
var segments = location.pathname.split("/").filter(Boolean);
var skip = CONFIG.pathSegmentsToSkip;
var slugSegments = segments.slice(skip);
var slug = decodeURIComponent(slugSegments.join("/"));
var homepage =
location.protocol +
"//" +
location.hostname +
(location.port ? ":" + location.port : "") +
"/" +
segments.slice(0, skip).join("/");
var slugKey = normalizeKey(slug);
fetchPage(1, slugKey, homepage, function (issue) {
if (!issue) {
location.replace(homepage);
return;
}
handleMatch(issue, homepage);
});
}
redirect();
</script>
</head>
<body></body>
</html>