-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebring.js
More file actions
116 lines (104 loc) · 3.06 KB
/
Copy pathwebring.js
File metadata and controls
116 lines (104 loc) · 3.06 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const DATA_FOR_WEBRING = `https://raw.githubusercontent.com/BitcoinCash1/bch-webring/refs/heads/main/webring.json`;
const template = document.createElement("template");
template.innerHTML = `
<style>
:host {
display: block;
font: 100% system-ui, sans-serif;
color-scheme: light dark;
}
.webring {
border: 2px solid #09ba8a;
border-radius: 8px;
padding: 1.25rem 1.5rem;
background: light-dark(#f0fdf8, #0d2b22);
color: light-dark(#0a3d2e, #d1f5ea);
text-align: center;
}
.webring h2 {
margin: 0 0 0.5rem;
font-size: 1rem;
font-weight: 600;
letter-spacing: 0.05em;
text-transform: uppercase;
color: #09ba8a;
}
.webring p {
margin: 0.25rem 0;
font-size: 0.95rem;
}
.webring a {
color: #09ba8a;
text-decoration: none;
font-weight: 500;
}
.webring a:hover {
text-decoration: underline;
}
.nav {
margin-top: 0.75rem;
display: flex;
justify-content: center;
gap: 1rem;
}
.nav a {
background: #09ba8a;
color: #fff;
padding: 0.3rem 0.85rem;
border-radius: 4px;
font-size: 0.875rem;
}
.nav a:hover {
background: #07a377;
text-decoration: none;
}
</style>
<div class="webring">
<div id="copy"></div>
</div>`;
class WebRing extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
const thisSite = this.getAttribute("site");
fetch(DATA_FOR_WEBRING)
.then((response) => response.json())
.then((sites) => {
const matchedSiteIndex = sites.findIndex(
(site) => site.url === thisSite
);
if (matchedSiteIndex === -1) {
this.shadowRoot.querySelector("#copy").innerHTML =
`<p>Site not found in the webring.</p>`;
return;
}
const matchedSite = sites[matchedSiteIndex];
const prevSiteIndex =
matchedSiteIndex === 0 ? sites.length - 1 : matchedSiteIndex - 1;
const nextSiteIndex =
matchedSiteIndex === sites.length - 1 ? 0 : matchedSiteIndex + 1;
const randomSiteIndex =
sites.length > 1
? this.getRandomIndexExcluding(matchedSiteIndex, sites.length)
: matchedSiteIndex;
this.shadowRoot.querySelector("#copy").innerHTML = `
<h2>BCH Webring</h2>
<p>You are visiting <a href="${matchedSite.url}">${matchedSite.name}</a> by ${matchedSite.owner}</p>
<nav class="nav">
<a href="${sites[prevSiteIndex].url}">← Prev</a>
<a href="${sites[randomSiteIndex].url}">Random</a>
<a href="${sites[nextSiteIndex].url}">Next →</a>
</nav>`;
});
}
getRandomIndexExcluding(excludeIndex, length) {
const random = this.getRandomInt(0, length - 2);
return random >= excludeIndex ? random + 1 : random;
}
getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
window.customElements.define("bch-webring", WebRing);