x0/modules/http.js

24 lines
903 B
JavaScript
Raw Normal View History

2023-12-24 00:32:43 +01:00
// [x-get="<url>"]
// [x-inner-text] - will use inner text instead of html
(() => {
let storage = window.x0?.registerStorage("http") || {};
storage.intervals = [];
window.x0?.registerModuleRemoveHook("http", ({ intervals }) => intervals.forEach(p => clearInterval(p)));
document.querySelectorAll("[x-get]")
.forEach(async e => {
e.innerHTML += "*";
let update = async () => {
let result = await (await fetch(e.getAttribute("x-get"))).text();
if (e.hasAttribute("x-inner-text")) {
e.innerText = result;
} else {
e.innerHTML = result;
}
}
await update()
if (e.hasAttribute("x-get-refresh")) {
storage.intervals.push(setInterval(update, e.getAttribute("x-get-refresh")));
}
})
})()