labolyon.fr/js/status.js

42 lines
1.1 KiB
JavaScript

/**
* Fetch status content and insert main content into provided container
* @param {Element} containerEl Container to insert status into
*/
async function insertStatus(containerEl){
const baseUrl = containerEl.href;
const res = await fetch(baseUrl, {cache:"no-cache"});
if(!res.ok){
console.info(`Network error requesting status : ${res.status} ${res.statusText}`);
return;
}
const dom = new DOMParser().parseFromString(await res.text(), "text/html");
let base = dom.createElement("base");
base.href = containerEl.href;
dom.head.append(base);
const main = dom.querySelector("main");
if(!main)
throw new Error("Status page does not contain a <main> element");
for(let it of main.querySelectorAll("[href],[src]")){
if(it.hasAttribute("href")){
it.href = it.href
} else {
it.src = it.src
}
}
containerEl.innerHTML = "";
for(let node of main.childNodes){
containerEl.append(document.importNode(node, true));
}
containerEl.className = main.className;
}
insertStatus(document.getElementById("status"))