transbeam/static/js/download.js

30 lines
1.1 KiB
JavaScript

let updateInterval;
document.addEventListener("DOMContentLoaded", () => {
const downloadContents = document.getElementById("download_contents");
if (!downloadContents) { return; }
const table = downloadContents.getElementsByTagName("tbody")[0];
if (table.children.length === 0) { return; }
updateInterval = setInterval(() => {
fetch(`info?code=${CODE}`)
.then((res) => {
if (res.status === 404) {
clearInterval(updateInterval);
return Promise.reject("Download not found");
} else {
return res.json();
}
})
.then((info) => {
for (const [index, offset] of info.offsets.entries()) {
table.children[index].className = (offset > info.available) ? "unavailable" : "";
}
if (info.offsets.length === 0 || info.available >= info.offsets.at(-1)) {
clearInterval(updateInterval);
}
})
.catch(console.error);
}, 5000);
});