-
Notifications
You must be signed in to change notification settings - Fork 19
/
webring.js
90 lines (70 loc) · 2.09 KB
/
webring.js
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
---
---
document.write('<div id="ctwebring"></div>');
function showWebring(useStyles) {
let elm = 'ctwebring';
const url = '{{ "list.json" | absolute_url }}';
let req = new XMLHttpRequest();
req.open('GET', url, true);
req.responseType = 'json';
req.onload = function() {
if(req.status != 200) {
console.error('Webring: Unable to load', url);
return;
}
if(!req.response) {
console.error('Webring: No response returned', url);
}
doShow(req.response, elm);
}
function doShow(response, elm) {
let sites = response.sites;
let current;
for(let i = 0; i < sites.length; i++) {
if(window.location.href.substr(0, sites[i].url.length) === sites[i].url) {
current = i;
break;
}
}
if(typeof current === 'undefined') {
current = 0;
}
let next = sites[((current + 1) % sites.length)];
let prev = sites[((current + sites.length - 1) % sites.length)];
document.getElementById(elm).innerHTML = `<div class="CTW-container">
<div class="CTW-intro">
This website is part of the <strong><a href="{{ '/' | absolute_url }}">{{ site.title }}</a></strong>.
</div>
<nav class="CTW-nav"><ul>
<li class="CTW-link CTW-link-prev">
<span class="CTW-link-direction">←</span>
<a href="${prev.url}" class="CTW-link-name">${prev.name}</a>
</a></li>
<li class="CTW-link CTW-link-next">
<a href="${next.url}" class="CTW-link-name">${next.name}</a>
<span class="CTW-link-direction">→</span>
</a></li>
</ul></nav>
</div>`;
if(useStyles) {
let style = document.createElement('style');
style.appendChild(document.createTextNode(`
.CTW-intro {
margin-bottom: 1rem;
}
.CTW-nav {
list-style: none;
display: flex;
}
.CTW-link {
display: inline-block;
}
.CTW-link:not(:last-child) {
margin-right: 2rem;
}
`));
document.getElementById(elm).appendChild(style);
}
}
req.send();
}