blob: 598de5c68c2fd61748aeb8896113c8d5069906e1 (
plain)
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
|
function setupEvents() {
document.querySelectorAll<HTMLElement>(".tag")?.forEach(t => {
t.addEventListener("click", _ => {
t.classList.toggle("active")
})
})
document.querySelectorAll<HTMLAnchorElement>("a")?.forEach(a => {
a.addEventListener("click", e => {
if (!a.href.startsWith(window.location.origin)) return
e.preventDefault()
changePage(a.href)
})
})
}
function changePage(href: string) {
fetch(href)
.then(resp => resp.text())
.then(html => {
const doc = new DOMParser().parseFromString(html, "text/html")
window.history.pushState({}, "", href)
document.title = doc.title
const distMain = doc.querySelector("main")
const currentMain = document.querySelector("main")
if (distMain === null || currentMain === null) document.body = doc.body
else currentMain.innerHTML = distMain.innerHTML
setupEvents()
})
}
window.addEventListener("popstate", e => window.location.reload())
setupEvents()
|