blob: 68b6f1916d369e0d543197706016a67357ebd08e (
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
34
35
36
37
38
39
40
|
function setupEvents() {
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
document.body = doc.body
document.head.querySelectorAll("style").forEach(e => document.head.removeChild(e))
doc.head.querySelectorAll("style").forEach(e => {
document.head.appendChild(e)
})
setupEvents()
scrollTo({
top: 0,
left: 0,
behavior: "smooth"
})
})
}
window.addEventListener("popstate", _ => {
window.location.reload()
scrollTo({
top: 0,
left: 0,
behavior: "smooth"
})
})
setupEvents()
|