-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect.js
30 lines (27 loc) · 882 Bytes
/
redirect.js
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
function tryto(func){
return new Promise(async (resolve) => {
try{
var r = await func();
resolve(r);
} catch (e){
resolve(e);
}
})
}
document.addEventListener('DOMContentLoaded', async function (event) {
var func = async () => {
var redirections = await fetch('https://samuellouf.github.io/api/samuellouf-website/v1/redirections.json').then((r) => r.json());
const isURLCompatible = (origin) => {
return window.location.href.startsWith(origin);
}
const getNewURL = (origin, redirection) => {
return redirection + window.location.href.replace(origin + '/', '') + (window.location.href.replace(origin, '')[1] != '/' ? '/' : '');
}
for (var url of redirections){
if (isURLCompatible(url.origin)){
window.location.href = getNewURL(url.origin, url.redirection);
}
}
}
tryto(func);
})