2024-04-16 20:24:05 +00:00
|
|
|
const swap_event = new Event("swap");
|
2024-04-16 16:53:24 +00:00
|
|
|
|
2024-04-16 20:24:05 +00:00
|
|
|
let caches = {};
|
2024-04-16 16:53:24 +00:00
|
|
|
|
2024-04-17 16:08:53 +00:00
|
|
|
async function cached_fetch(url) {
|
|
|
|
let cached = caches[url];
|
2024-04-16 20:24:05 +00:00
|
|
|
|
2024-04-17 16:08:53 +00:00
|
|
|
const res = cached === undefined ? await fetch(url) : await fetch(url, {
|
|
|
|
headers: {
|
|
|
|
"If-Modified-Since": cached.last_modified
|
|
|
|
}
|
|
|
|
});
|
2024-04-16 20:24:05 +00:00
|
|
|
|
2024-04-17 16:08:53 +00:00
|
|
|
if (res.status === 304 && cached !== undefined) {
|
|
|
|
return cached.content;
|
|
|
|
}
|
|
|
|
if (res.status !== 200) return;
|
|
|
|
if (!res.headers.get("content-type").startsWith("text/html")) return;
|
|
|
|
|
|
|
|
const text = await res.text();
|
|
|
|
if (res.headers.get("last-modified")) {
|
|
|
|
caches[url] = {
|
|
|
|
content: text,
|
|
|
|
last_modified: res.headers.get("last-modified")
|
|
|
|
}
|
2024-04-16 20:24:05 +00:00
|
|
|
}
|
2024-04-17 16:08:53 +00:00
|
|
|
return text;
|
2024-04-16 20:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-04-17 16:08:53 +00:00
|
|
|
const text = await cached_fetch(url);
|
2024-04-16 16:53:24 +00:00
|
|
|
const content = new DOMParser().parseFromString(text, "text/html");
|
|
|
|
|
|
|
|
const stylesheets = [...content.querySelectorAll("link[rel='stylesheet']")];
|
|
|
|
|
|
|
|
// swap title
|
|
|
|
document.title = content.title;
|
|
|
|
// swap body html
|
|
|
|
document.body.innerHTML = content.body.innerHTML;
|
|
|
|
// swap stylesheets
|
2024-04-16 20:24:05 +00:00
|
|
|
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();
|
|
|
|
});
|
2024-04-16 16:53:24 +00:00
|
|
|
// push history
|
2024-04-16 20:24:05 +00:00
|
|
|
if (stateful) history.pushState(url, "", url);
|
2024-04-16 16:53:24 +00:00
|
|
|
|
|
|
|
bind(document.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bind(content) {
|
2024-04-16 20:24:05 +00:00
|
|
|
if (typeof content !== 'object' || content.nodeType !== Node.ELEMENT_NODE) return;
|
2024-04-16 16:53:24 +00:00
|
|
|
|
2024-04-16 20:24:05 +00:00
|
|
|
content.querySelectorAll("[swap-url]").forEach(element => {
|
|
|
|
const href = element.attributes.getNamedItem('swap-url').value;
|
2024-04-16 16:53:24 +00:00
|
|
|
|
|
|
|
element.addEventListener("click", event => {
|
|
|
|
event.preventDefault();
|
2024-04-16 20:24:05 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
});
|
2024-04-16 16:53:24 +00:00
|
|
|
});
|
|
|
|
|
2024-04-16 20:24:05 +00:00
|
|
|
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);
|
|
|
|
if (href.includes(".")) return;
|
2024-04-16 16:53:24 +00:00
|
|
|
|
|
|
|
element.addEventListener("click", event => {
|
|
|
|
event.preventDefault();
|
2024-04-16 20:24:05 +00:00
|
|
|
swap(element.href, true);
|
2024-04-16 16:53:24 +00:00
|
|
|
});
|
|
|
|
});
|
2024-04-16 20:24:05 +00:00
|
|
|
|
|
|
|
document.dispatchEvent(swap_event);
|
2024-04-16 16:53:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 20:24:05 +00:00
|
|
|
window.addEventListener("popstate", event => {
|
|
|
|
swap(event.state, false);
|
|
|
|
});
|
|
|
|
|
2024-04-17 17:29:39 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
bind(document.body);
|
|
|
|
});
|