I FIXED ROUTING!!!!! YIPPEEEEEEE
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
13d802d361
commit
b42b37ff9c
11 changed files with 113 additions and 57 deletions
|
@ -1,10 +1,35 @@
|
|||
async function swap(url) {
|
||||
const res = await fetch(url);
|
||||
const swap_event = new Event("swap");
|
||||
|
||||
if (res.status !== 200) return;
|
||||
if (!res.headers.get("content-type").startsWith("text/html")) return;
|
||||
let caches = {};
|
||||
window.lmao = caches;
|
||||
|
||||
const text = await res.text();
|
||||
async function check_cache(url) {
|
||||
if (caches[url]) {
|
||||
return caches[url];
|
||||
} else {
|
||||
const res = await fetch(url);
|
||||
|
||||
if (res.status !== 200) return;
|
||||
if (!res.headers.get("content-type").startsWith("text/html")) return;
|
||||
|
||||
const text = await res.text();
|
||||
caches[url] = text;
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
async function swap(url, stateful) {
|
||||
if (typeof url !== 'string') return;
|
||||
|
||||
const segments = window.location.href.split("/");
|
||||
if (url.startsWith(window.location.origin) && segments[segments.length - 1].includes("#")) {
|
||||
window.location.href = url;
|
||||
return;
|
||||
}
|
||||
|
||||
if (stateful && window.location.href.endsWith(url)) return;
|
||||
|
||||
const text = await check_cache(url);
|
||||
const content = new DOMParser().parseFromString(text, "text/html");
|
||||
|
||||
const stylesheets = [...content.querySelectorAll("link[rel='stylesheet']")];
|
||||
|
@ -13,22 +38,53 @@ async function swap(url) {
|
|||
document.title = content.title;
|
||||
// swap body html
|
||||
document.body.innerHTML = content.body.innerHTML;
|
||||
// swap head html
|
||||
document.head.innerHTML = content.head.innerHTML;
|
||||
// swap stylesheets
|
||||
// const old_sheets = document.head.querySelectorAll("link[rel='stylesheet']");
|
||||
// stylesheets.forEach(stylesheet => { document.head.appendChild(stylesheet) });
|
||||
// old_sheets.forEach(stylesheet => { stylesheet.remove(); });
|
||||
const old_sheets = document.head.querySelectorAll("link[rel='stylesheet']");
|
||||
stylesheets.forEach(stylesheet => {
|
||||
let exists = false;
|
||||
old_sheets.forEach(old_sheet => {
|
||||
if (old_sheet.href === stylesheet.href) exists = true;
|
||||
});
|
||||
if (!exists) document.head.appendChild(stylesheet);
|
||||
});
|
||||
old_sheets.forEach(old_sheet => {
|
||||
let exists = false;
|
||||
stylesheets.forEach(stylesheet => {
|
||||
if (stylesheet.href === old_sheet.href) exists = true;
|
||||
});
|
||||
if (!exists) old_sheet.remove();
|
||||
});
|
||||
// push history
|
||||
window.history.pushState({}, "", url);
|
||||
if (stateful) history.pushState(url, "", url);
|
||||
|
||||
bind(document.body);
|
||||
}
|
||||
|
||||
function bind(content) {
|
||||
if (typeof(content) !== 'object' || content.nodeType !== Node.ELEMENT_NODE) return;
|
||||
if (typeof content !== 'object' || content.nodeType !== Node.ELEMENT_NODE) return;
|
||||
|
||||
content.querySelectorAll("a[href]").forEach(element => {
|
||||
content.querySelectorAll("[swap-url]").forEach(element => {
|
||||
const href = element.attributes.getNamedItem('swap-url').value;
|
||||
|
||||
element.addEventListener("click", event => {
|
||||
event.preventDefault();
|
||||
swap(href, true);
|
||||
});
|
||||
|
||||
[...element.querySelectorAll("a[href], [swap-url]")].forEach(element => {
|
||||
if (element.href) {
|
||||
if (!element.href.endsWith(href)) return;
|
||||
element.attributes.removeNamedItem("href");
|
||||
}
|
||||
const swap_url = element.attributes.getNamedItem("swap-url");
|
||||
if (swap_url) {
|
||||
if (!swap_url.endsWith(href)) return;
|
||||
element.attributes.removeNamedItem("swap-url");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
content.querySelectorAll("a[href]:not([swap-url])").forEach(element => {
|
||||
if (element.href.includes("#")) return;
|
||||
if (!element.href.startsWith(window.location.origin)) return;
|
||||
const href = element.href.substring(window.location.origin.length);
|
||||
|
@ -36,18 +92,15 @@ function bind(content) {
|
|||
|
||||
element.addEventListener("click", event => {
|
||||
event.preventDefault();
|
||||
swap(element.href);
|
||||
})
|
||||
});
|
||||
|
||||
content.querySelectorAll("[swap-url]").forEach(element => {
|
||||
const href = element.attributes.getNamedItem('swap-url').value;
|
||||
|
||||
element.addEventListener("click", event => {
|
||||
event.preventDefault();
|
||||
swap(href);
|
||||
swap(element.href, true);
|
||||
});
|
||||
});
|
||||
|
||||
document.dispatchEvent(swap_event);
|
||||
}
|
||||
|
||||
window.addEventListener("popstate", event => {
|
||||
swap(event.state, false);
|
||||
});
|
||||
|
||||
bind(document.body);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue