class UptimeCard extends HTMLElement { constructor() { super(); this.timer = null; } connectedCallback() { this.innerHTML = `

Live uptime

Auto refresh
--s
Disconnected
`; window.addEventListener('app:login', event => this.start(event.detail.token)); window.addEventListener('app:logout', () => { this.stop(); this.setState('Disconnected', false); this.querySelector('#seconds').textContent = '--'; }); window.addEventListener('beforeunload', () => this.stop()); if (window.App && window.App.token()) this.start(window.App.token()); } disconnectedCallback() { this.stop(); } setState(text, connected) { this.querySelector('#state').textContent = text; this.querySelector('#dot').classList.toggle('on', !!connected); } start(token) { this.stop(); if (!token) { this.setState('Unavailable', false); return; } this.setState('Loading', false); const load = async () => { try { const text = await window.App.serverReq('GET', '/api/uptime'); const json = JSON.parse(text); if (!json.success) { this.setState('Unavailable', false); return; } this.querySelector('#seconds').textContent = json.uptimeSeconds; this.setState('Connected', true); } catch (error) { this.setState('Retrying', false); } }; load(); this.timer = setInterval(load, 5000); } stop() { if (this.timer) clearInterval(this.timer); this.timer = null; } } customElements.define('uptime-card', UptimeCard);