-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettyurl.js
31 lines (28 loc) · 933 Bytes
/
prettyurl.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
31
// TODO Test Me
document.addEventListener('DOMContentLoaded', function () {
const links = document.querySelectorAll('a');
links.forEach(link => {
const newUrl = prettyUrl(link.href);
link.href = newUrl;
link.addEventListener('click', function (event) {
event.preventDefault();
window.location.href = newUrl;
});
});
// Redirect to pretty URL if the current URL is ugly
const currentUrl = window.location.href;
const prettyCurrentUrl = prettyUrl(currentUrl);
if (currentUrl !== prettyCurrentUrl) {
window.location.href = prettyCurrentUrl;
}
});
function prettyUrl(url) {
if (url.includes('wesmun') || url.includes('github')) {
if (url.endsWith('index.html')) {
url = url.replace('index.html', '');
} else if (url.endsWith('.html')) {
url = url.slice(0, -5);
}
}
return url;
}