This commit is contained in:
2026-04-25 16:44:08 +09:00
parent 9d61a19a6e
commit 5a2c2629da
3 changed files with 61 additions and 27 deletions
+48 -21
View File
@@ -91,30 +91,28 @@
newHead.querySelectorAll('style').forEach(s => head.appendChild(s.cloneNode(true)));
}
document.addEventListener('click', (event) => {
const link = event.target.closest('a');
if (!link || link.hasAttribute('download')) return;
let abortController = null;
const url = new URL(link.href, location.href);
if (url.origin !== location.origin) return;
if (link.target || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
if (url.hash && url.pathname === location.pathname) {
event.preventDefault();
const target = document.querySelector(url.hash);
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
history.pushState(null, '', url.hash);
}
return;
}
event.preventDefault();
async function navigate(url) {
if (abortController) abortController.abort();
const ac = new AbortController();
abortController = ac;
document.startViewTransition(async () => {
const response = await fetch(url.href, {
headers: { 'X-Requested-With': 'view-transition' }
});
let response;
try {
response = await fetch(url.href, {
headers: { 'X-Requested-With': 'view-transition' },
signal: ac.signal
});
} catch (err) {
if (err.name === 'AbortError') return;
location.href = url.href;
return;
}
if (ac.signal.aborted) return;
const html = await response.text();
const doc = new DOMParser().parseFromString(html, 'text/html');
@@ -145,6 +143,35 @@
window.__cursorReinit();
}
});
}
window.__navigate = function (href) {
let url;
try { url = new URL(href, location.href); } catch (_) { location.href = href; return; }
if (url.origin !== location.origin) { location.href = href; return; }
navigate(url);
};
document.addEventListener('click', (event) => {
const link = event.target.closest('a');
if (!link || link.hasAttribute('download')) return;
const url = new URL(link.href, location.href);
if (url.origin !== location.origin) return;
if (link.target || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
if (url.hash && url.pathname === location.pathname) {
event.preventDefault();
const target = document.querySelector(url.hash);
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
history.pushState(null, '', url.hash);
}
return;
}
event.preventDefault();
navigate(url);
});
window.addEventListener('popstate', () => location.reload());