const router = document.querySelector("#router") const path = typeof router.getAttribute('dir') === 'string' ? router.getAttribute('dir') : 'pages' const page_404 = `
Page not found
` window.addEventListener("DOMContentLoaded", onRouteChange) window.addEventListener("hashchange", onRouteChange) async function checkPath(link) { let response = await fetch(link) return !(response.status === 404) } function getPathname() { let pathname = window.location.pathname if (pathname.endsWith("index.html")) { pathname = pathname.substring(0, pathname.length - 10) } return pathname } function onRouteChange() { let hashLocation if (window.location.hash.substring(1, 2) == "/") { hashLocation = window.location.hash.substring(2) } else { hashLocation = window.location.hash.substring(1) } if (!hashLocation) { hashLocation = "index" } loadContent(hashLocation) } async function loadContent(uri) { if (!(await fetchData(`${path}/${uri}.html`))) { updatePage(page_404) } } async function fetchData(link) { let response = await fetch(link) if (!response.ok) { return false } let content = await response.text() // Get ", styleStart + 8) styleText = content.substring(styleStart + 8, styleEnd) content = content.replace("", "") } // Get ", scriptStart + 9) scriptText = content.substring(scriptStart + 9, scriptEnd) content = content.replace("", "") } // Get all linked stylesheets const links = content.match(/]+>/g) || "" if (links) { links.forEach((link) => { let href = link.match(/href=".*"/g) if (href.length === 0) { return } href = href[0] if (href.length === 7) { return } const newLink = document.createElement("link") newLink.href = href.substring(6, href.length - 1) newLink.rel = "stylesheet" router.appendChild(newLink) content.replace(link, "") }) } // Get all script tags with src attribute const scripts = content.match(/