Actualiser webring.js
This commit is contained in:
parent
c0b80ebef2
commit
0c1bc78cc2
1 changed files with 24 additions and 18 deletions
42
webring.js
42
webring.js
|
|
@ -1,4 +1,4 @@
|
|||
const JSON_WEBRING = "https://codeberg.org/gablaxy/webring/raw/branch/main/webring.json";
|
||||
const JSON_WEBRING = "https://corsproxy.io/?https://gablaxy.codeberg.page/webring.json";
|
||||
|
||||
const template = document.createElement("template");
|
||||
template.innerHTML = `
|
||||
|
|
@ -54,31 +54,37 @@ class Webring extends HTMLElement {
|
|||
this.shadowRoot.appendChild(template.content.cloneNode(true));
|
||||
|
||||
const currentSite = this.getAttribute("site");
|
||||
// ou sinon on peut aller chercher le paramètre site dans la déclaration du widget
|
||||
|
||||
fetch(JSON_WEBRING)
|
||||
.then((response) => response.json())
|
||||
.then((sites) => {
|
||||
// Va chercher les infos du site actuel dans le json
|
||||
const normalize = (url) => url.replace(/\/$/, "");
|
||||
|
||||
const matchedSiteIndex = sites.findIndex(
|
||||
(site) => site.url === currentSite
|
||||
)
|
||||
(site) => normalize(site.url) === normalize(currentSite)
|
||||
);
|
||||
|
||||
if (matchedSiteIndex === -1) {
|
||||
this.shadowRoot.querySelector("#webring-inner").insertAdjacentHTML("afterbegin", `
|
||||
<h3 id="webring-title">grossomodo</h3>
|
||||
<p>T'es pas dans le gang bozo</p>
|
||||
`);
|
||||
return;
|
||||
}
|
||||
|
||||
// stocke les index des sites avant et après dans le json
|
||||
let previousSiteIndex = matchedSiteIndex - 1;
|
||||
if(previousSiteIndex < 0)previousSiteIndex = sites.length - 1;
|
||||
|
||||
let nextSiteIndex = matchedSiteIndex + 1;
|
||||
if(nextSiteIndex >= sites.length)nextSiteIndex = 0;
|
||||
if (previousSiteIndex < 0) previousSiteIndex = sites.length - 1;
|
||||
|
||||
let nextSiteIndex = matchedSiteIndex + 1;
|
||||
if (nextSiteIndex >= sites.length) nextSiteIndex = 0;
|
||||
|
||||
|
||||
const content = `
|
||||
<h3 id="webring-title">grossomodo</h3>
|
||||
<a href="${sites[previousSiteIndex].url}" rel="prev noreferrer external">< avant</a>
|
||||
/
|
||||
<a rel="external noreferrer" href="${sites[matchedSiteIndex].url}">${sites[matchedSiteIndex].name}</a>
|
||||
/
|
||||
<a href="${sites[nextSiteIndex].url}" rel="next noreferrer external">après ></a>
|
||||
<h3 id="webring-title">grossomodo</h3>
|
||||
<a href="${sites[previousSiteIndex].url}" rel="prev noreferrer external">< avant</a>
|
||||
/
|
||||
<a rel="external noreferrer" href="${sites[matchedSiteIndex].url}">${sites[matchedSiteIndex].name}</a>
|
||||
/
|
||||
<a href="${sites[nextSiteIndex].url}" rel="next noreferrer external">après ></a>
|
||||
`;
|
||||
|
||||
this.shadowRoot
|
||||
|
|
@ -99,4 +105,4 @@ class Webring extends HTMLElement {
|
|||
}
|
||||
}
|
||||
|
||||
window.customElements.define("webring-iutechno", Webring);
|
||||
window.customElements.define("webring-iutechno", Webring);
|
||||
Loading…
Reference in a new issue