forge
Explore
Log in

Don't you dare go Hollow

personality

Clone
★ 0

0 watching0 forksRustGPL-3.0460.1 KiB4de8815Tahinli7 days ago22 commits

nav.js136 lines, 4.5 KiBHistoryRawDownload
1(function () {
2 'use strict';
3 if (window.__personalityNav) { return; }
4 window.__personalityNav = true;
5
6 var root = document.documentElement;
7 var FILE_PATH = /^\/(fonts\/|main\.css$|nav\.js$|favicon\.ico$)/;
8 var TTL = 30000;
9 var HOVER_DELAY = 50;
10 var cache = new Map();
11 var latest = 0;
12 var inFlight = 0;
13 var shown = location.pathname + location.search;
14 var hoverTimer = null;
15 var hoverLink = null;
16
17 function local(url) {
18 return url.origin === location.origin && !FILE_PATH.test(url.pathname);
19 }
20
21 function linkUrl(a) {
22 var href = a.getAttribute('href');
23 if (href.charAt(0) !== '/' || href.charAt(1) === '/') { return null; }
24 var url = new URL(href, location.href);
25 return local(url) ? url : null;
26 }
27
28 function linkOf(node) {
29 return node && node.closest ? node.closest('a[href]') : null;
30 }
31
32 function canPrefetch() {
33 return !(navigator.connection && navigator.connection.saveData);
34 }
35
36 function fetchText(href) {
37 return fetch(href, { headers: { accept: 'text/html' } }).then(function (res) {
38 var type = res.headers.get('content-type') || '';
39 var final = new URL(res.url || href);
40 if (!res.ok || !/^\s*text\/html/i.test(type) || final.origin !== location.origin) {
41 throw new Error('not soft');
42 }
43 return res.text();
44 });
45 }
46
47 function prefetch(href) {
48 var key = href.split('#')[0];
49 var hit = cache.get(key);
50 if (hit && Date.now() - hit.at < TTL) { return hit.promise; }
51 var promise = fetchText(key);
52 cache.set(key, { promise: promise, at: Date.now() });
53 promise.catch(function () {
54 if (cache.get(key) && cache.get(key).promise === promise) { cache.delete(key); }
55 });
56 return promise;
57 }
58
59 function placeHash(hash) {
60 var el = hash.length > 1 ? document.getElementById(hash.slice(1)) : null;
61 if (el) { el.scrollIntoView(); }
62 return !!el;
63 }
64
65 function swap(html, url, pop, y) {
66 var doc = new DOMParser().parseFromString(html, 'text/html');
67 if (!doc.body) { throw new Error('no body'); }
68 document.title = doc.title;
69 var fresh = document.importNode(doc.body, true);
70 document.body.replaceChildren.apply(document.body, Array.prototype.slice.call(fresh.childNodes));
71 if (!pop) { history.pushState(null, '', url.href); }
72 shown = url.pathname + url.search;
73 if (!placeHash(url.hash)) { window.scrollTo(0, pop ? y : 0); }
74 }
75
76 function go(url, pop, y) {
77 var mark = ++latest;
78 if (!pop) { history.replaceState({ y: window.scrollY }, '', location.href); }
79 inFlight += 1;
80 root.setAttribute('aria-busy', 'true');
81 prefetch(url.href).then(function (html) {
82 if (mark === latest) { swap(html, url, pop, y); }
83 }).catch(function () {
84 if (mark === latest) { location.assign(url.href); }
85 }).then(function () {
86 inFlight -= 1;
87 if (inFlight === 0) { root.removeAttribute('aria-busy'); }
88 });
89 }
90
91 document.addEventListener('click', function (e) {
92 if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.defaultPrevented) { return; }
93 var a = linkOf(e.target);
94 if (!a || a.hasAttribute('target') || a.hasAttribute('download')) { return; }
95 var url = linkUrl(a);
96 if (!url) { return; }
97 if (url.pathname + url.search === shown && url.hash !== '') { return; }
98 e.preventDefault();
99 go(url, false, 0);
100 });
101
102 document.addEventListener('mouseover', function (e) {
103 var a = linkOf(e.target);
104 if (!a || a === hoverLink) { return; }
105 clearTimeout(hoverTimer);
106 hoverTimer = null;
107 hoverLink = a;
108 var url = linkUrl(a);
109 if (!url || !canPrefetch()) { return; }
110 hoverTimer = setTimeout(function () {
111 hoverTimer = null;
112 prefetch(url.href).catch(function () {});
113 }, HOVER_DELAY);
114 }, true);
115
116 document.addEventListener('mouseout', function (e) {
117 var a = linkOf(e.target);
118 if (!a || a !== hoverLink || (e.relatedTarget && a.contains(e.relatedTarget))) { return; }
119 clearTimeout(hoverTimer);
120 hoverTimer = null;
121 hoverLink = null;
122 }, true);
123
124 document.addEventListener('touchstart', function (e) {
125 var a = linkOf(e.target);
126 var url = a && canPrefetch() ? linkUrl(a) : null;
127 if (url) { prefetch(url.href).catch(function () {}); }
128 }, { passive: true, capture: true });
129
130 window.addEventListener('popstate', function (e) {
131 var url = new URL(location.href);
132 if (!local(url)) { location.reload(); return; }
133 if (url.pathname + url.search === shown) { placeHash(url.hash); return; }
134 go(url, true, e.state && e.state.y ? e.state.y : 0);
135 });
136})();