-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (76 loc) · 2.71 KB
/
index.js
File metadata and controls
92 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const normalizeUrl = require('normalize-url')
window.normalizeUrl = normalizeUrl
/**
* Remove the protocol from a url
* @param {string}} url The url to remove the protocl from
*/
function withoutProtocol (url) {
return url.split('//').slice(1).join('')
}
/**
* Prettify a URL, removing trailing slash and protocol
* @param {string} url The url to prettify
*/
function prettifyUrl (url) {
return withoutProtocol(normalizeUrl(url))
}
/**
* Compare whether two urls are equal after normalizing them
* @param {string} a The first url to compare
* @param {string} b The second url to compare
*/
function compareUrls (a, b) {
if (a === b) {
return true
}
return prettifyUrl(a) === prettifyUrl(b)
}
function onceReady (fn) {
if (document.readyState !== 'loading') fn()
else document.addEventListener('DOMContentLoaded', fn)
}
/**
* Returns [url, index]
* @param {*} from The from parameter
*/
function validFromUrl (from) {
let sites = [...document.querySelectorAll('.ring ol li a')]
const current_site_anchor = sites.find(site => compareUrls(site.href, from))
return [current_site_anchor.href, sites.indexOf(current_site_anchor)]
}
function getPreviousSite(current_index) {
return getSiteRelative(current_index, -1)
}
function getNextSite(current_index) {
return getSiteRelative(current_index, 1)
}
/**
* Return the site in the web ring offset by `delta`
* @param {*} current_index The current index in the web ring of sites
* @param {*} delta How far forward/backward to jump
*/
function getSiteRelative(current_index, delta) {
let sites = [...document.querySelectorAll('.ring ol li a')]
return sites[(current_index + delta + sites.length) % sites.length].href
}
onceReady(() => {
let params = Object.fromEntries(new URLSearchParams(location.search))
let [current_site, current_index] = validFromUrl(params.from)
if (current_site) {
// render the navigation backwards link
let goBack = document.querySelector('.go-back')
goBack.classList.remove('hidden')
goBack.innerText += ' to ' + prettifyUrl(params.from)
goBack.href = current_site
// render the navigation forwards link
let goForward = document.querySelector('.go-forward')
const nextSite = getNextSite(current_index)
goForward.classList.remove('hidden')
goForward.innerText = goForward.innerText.replace('$next', prettifyUrl(nextSite))
goForward.href = nextSite
// Handle redirects included in the url query
if (params.to === 'next')
window.location.href = (getNextSite(current_index));
else if (params.to === 'prev') window.location.href = getPreviousSite(current_index)
}
})