(function () { 'use strict'; if (window.__personalityNav) { return; } window.__personalityNav = true; var root = document.documentElement; var FILE_PATH = /^\/(fonts\/|main\.css$|nav\.js$|favicon\.ico$)/; var TTL = 30000; var HOVER_DELAY = 50; var cache = new Map(); var latest = 0; var inFlight = 0; var shown = location.pathname + location.search; var hoverTimer = null; var hoverLink = null; function local(url) { return url.origin === location.origin && !FILE_PATH.test(url.pathname); } function linkUrl(a) { var href = a.getAttribute('href'); if (href.charAt(0) !== '/' || href.charAt(1) === '/') { return null; } var url = new URL(href, location.href); return local(url) ? url : null; } function linkOf(node) { return node && node.closest ? node.closest('a[href]') : null; } function canPrefetch() { return !(navigator.connection && navigator.connection.saveData); } function fetchText(href) { return fetch(href, { headers: { accept: 'text/html' } }).then(function (res) { var type = res.headers.get('content-type') || ''; var final = new URL(res.url || href); if (!res.ok || !/^\s*text\/html/i.test(type) || final.origin !== location.origin) { throw new Error('not soft'); } return res.text(); }); } function prefetch(href) { var key = href.split('#')[0]; var hit = cache.get(key); if (hit && Date.now() - hit.at < TTL) { return hit.promise; } var promise = fetchText(key); cache.set(key, { promise: promise, at: Date.now() }); promise.catch(function () { if (cache.get(key) && cache.get(key).promise === promise) { cache.delete(key); } }); return promise; } function placeHash(hash) { var el = hash.length > 1 ? document.getElementById(hash.slice(1)) : null; if (el) { el.scrollIntoView(); } return !!el; } function swap(html, url, pop, y) { var doc = new DOMParser().parseFromString(html, 'text/html'); if (!doc.body) { throw new Error('no body'); } document.title = doc.title; var fresh = document.importNode(doc.body, true); document.body.replaceChildren.apply(document.body, Array.prototype.slice.call(fresh.childNodes)); if (!pop) { history.pushState(null, '', url.href); } shown = url.pathname + url.search; if (!placeHash(url.hash)) { window.scrollTo(0, pop ? y : 0); } } function go(url, pop, y) { var mark = ++latest; if (!pop) { history.replaceState({ y: window.scrollY }, '', location.href); } inFlight += 1; root.setAttribute('aria-busy', 'true'); prefetch(url.href).then(function (html) { if (mark === latest) { swap(html, url, pop, y); } }).catch(function () { if (mark === latest) { location.assign(url.href); } }).then(function () { inFlight -= 1; if (inFlight === 0) { root.removeAttribute('aria-busy'); } }); } document.addEventListener('click', function (e) { if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.defaultPrevented) { return; } var a = linkOf(e.target); if (!a || a.hasAttribute('target') || a.hasAttribute('download')) { return; } var url = linkUrl(a); if (!url) { return; } if (url.pathname + url.search === shown && url.hash !== '') { return; } e.preventDefault(); go(url, false, 0); }); document.addEventListener('mouseover', function (e) { var a = linkOf(e.target); if (!a || a === hoverLink) { return; } clearTimeout(hoverTimer); hoverTimer = null; hoverLink = a; var url = linkUrl(a); if (!url || !canPrefetch()) { return; } hoverTimer = setTimeout(function () { hoverTimer = null; prefetch(url.href).catch(function () {}); }, HOVER_DELAY); }, true); document.addEventListener('mouseout', function (e) { var a = linkOf(e.target); if (!a || a !== hoverLink || (e.relatedTarget && a.contains(e.relatedTarget))) { return; } clearTimeout(hoverTimer); hoverTimer = null; hoverLink = null; }, true); document.addEventListener('touchstart', function (e) { var a = linkOf(e.target); var url = a && canPrefetch() ? linkUrl(a) : null; if (url) { prefetch(url.href).catch(function () {}); } }, { passive: true, capture: true }); window.addEventListener('popstate', function (e) { var url = new URL(location.href); if (!local(url)) { location.reload(); return; } if (url.pathname + url.search === shown) { placeHash(url.hash); return; } go(url, true, e.state && e.state.y ? e.state.y : 0); }); })();