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
|
<!doctype HTML>
<html>
<head>
<style>
/* CSS taken from https://perfectmotherfuckingwebsite.com/ */
body{
max-width:650px;
margin:40px auto;
padding:0 10px;
font:18px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
color:#444
}
h1,h2,h3{
line-height:1.2
}
@media (prefers-color-scheme: dark){
body{
color:#c9d1d9;
background:#0d1117
}
a:link{
color:#58a6ff
}
a:visited{
color:#8e96f0
}
}
</style>
<script type="text/javascript">
let routes = {};
async function loadNav() {
let resp = await fetch("/routes.json");
routes = await resp.json();
let lis = Object.entries(routes).map(([key,route]) => {
let li = document.createElement("li");
let a = document.createElement("a");
let tn = document.createTextNode(route.title);
a.replaceChildren(tn);
a.href = "/"+key;
a.onclick = function(e) {
goto(key).then(() => {});
e.preventDefault();
};
li.replaceChildren(a);
return li;
})
let nav = document.getElementsByTagName("nav")[0];
nav.children[0].replaceChildren(...lis);
}
async function goto(key) {
let nextURL = new URL(window.location);
nextURL.pathname = "/"+key;
const nextTitle = routes[key].title;
const nextState = { additionalInformation: 'Updated the URL with JS' };
window.history.pushState(nextState, nextTitle, nextURL);
// Manually fire popstate :shrug:
// https://stackoverflow.com/questions/10940837/history-pushstate-does-not-trigger-popstate-event
let popStateEvent = new PopStateEvent('popstate', { state: nextState, title: nextTitle, location: nextURL });
dispatchEvent(popStateEvent);
}
async function loadContent() {
let key = window.location.pathname.substring(1);
let contentElem = document.getElementsByTagName("content")[0];
let head = document.getElementsByTagName("h1")[0];
let route = routes[key] ?? routes[""];
let contentFile = route.content;
head.innerHTML = route.title;
let resp = await fetch(contentFile);
let content = await resp.text();
contentElem.innerHTML = content;
}
window.addEventListener('popstate', async (event) => {
await loadContent();
});
window.addEventListener('load', async (event) => {
await loadNav();
await loadContent();
});
</script>
<title>Martin's Blog!</title>
</head>
<body>
<h1></h1>
<nav><ol></ol></nav>
<content></content>
</body>
</html>
|