const refresh_delay = 2 * 60 * 1000; // 2min let last_refresh = now(); function now() { return (new Date()).getTime(); } function enc(v) { return `${v}`.replace(/[\u00A0-\u9999<>&]/g, i => '&#'+i.charCodeAt(0)+';') } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function updateHTML(servers) { const main = document.getElementById('maincontent') let html = ''; for (const srv of servers) { html += ``; if (srv['icon'] === null) { html += `icon` } else { html += `icon` } html += `${enc(srv['name'])}` html += `${enc(srv['port'])}` html += ``; } main.innerHTML = html; } function onVisibilityChange() { console.log('[I] Visibility changed to ' + document.hidden) if (!document.hidden && (now() - last_refresh) > refresh_delay) { sleep(300).then(async () => await autoReload()); } } async function autoReload() { console.log('[I] AutoReload') try { document.getElementById('loader').classList.remove('hidden'); const res = await fetch('/api/v1/server'); if (res.status !== 200) { console.error(`status == ${res.status}`); return; } const data = await res.json(); updateHTML(data['servers']) } catch (err) { console.error(err); } finally { document.getElementById('loader').classList.add('hidden'); } } document.addEventListener("DOMContentLoaded", () => updateHTML(initialServers)); document.addEventListener('visibilitychange', onVisibilityChange); setInterval(autoReload, refresh_delay);