-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.min.js
1 lines (1 loc) · 1.14 KB
/
index.min.js
1
const o=window.location.origin;export function Init(){window.onload=()=>{document.querySelectorAll("a").forEach((e=>{e.hasAttribute("route")&&(e.onclick=t=>{t.preventDefault(),history.pushState({},"",e.href.replace(o,"")),window.dispatchEvent(new Event("popstate"))})}))}}export function State(e,t){Object.keys(t).forEach((n=>{document.querySelector(e).querySelectorAll(n).forEach((e=>{e.innerText=t[n]}))}))}export function Component(e,t){f(t).then((t=>{const n=document.querySelector(e);n.innerHTML=t[0],n.append(t[1])}))}export function Route(e,t){const n=document.querySelector(e);function r(){const e=window.location.href.replace(o,"");Object.keys(t).includes(e)?f(t[e]).then((e=>{n.innerHTML=e[0],n.append(e[1])})):n.innerHTML=""}r(),window.addEventListener("popstate",(()=>{r()}))}async function f(e){return fetch(e).then((e=>e.text())).then((e=>{const t=e.trim(),n=(new DOMParser).parseFromString(t,"text/html").body.children,o=document.createElement("script");return""!==n[1].innerHTML.trim()&&(o.innerText=n[1].innerText,o.setAttribute("type","module"),o.setAttribute("async",!0)),t.replace("/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi",""),[t,o]}))}