Admin: Refresh button + auto-refresh every minute, key kept in the browser so /admin reloads without it

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-30 14:09:35 -05:00
parent c35d118ace
commit 5a7dfb0828
3 changed files with 11 additions and 4 deletions
+1
View File
@@ -12,6 +12,7 @@
<div class="wrap" style="max-width:1100px">
<p class="logo" style="font-size:28px">Drip<span>Runner</span> <span style="font:800 12px var(--display);color:var(--muted);letter-spacing:.2em">ADMIN</span></p>
<p class="muted" id="links"></p>
<p style="display:flex;gap:12px;align-items:center;flex-wrap:wrap"><button class="btn ghost" id="refresh" type="button">Refresh</button><label style="margin:0;display:flex;gap:6px;align-items:center"><input type="checkbox" id="auto" style="width:auto"> auto every minute</label><span class="muted" id="stamp"></span></p>
<h2>Pool</h2>
<div class="grid" id="pool"></div>
<h2>Faucet</h2>
+9 -3
View File
@@ -1,8 +1,9 @@
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var KEY = new URLSearchParams(location.search).get('key') || ''; try { if (KEY) sessionStorage.setItem('dr.admin', KEY); else KEY = sessionStorage.getItem('dr.admin') || ''; } catch (e) {}
if (new URLSearchParams(location.search).get('key')) history.replaceState(null, '', '/admin');
var KEY = new URLSearchParams(location.search).get('key') || ''; try { if (KEY) localStorage.setItem('dr.admin', KEY); else KEY = localStorage.getItem('dr.admin') || ''; } catch (e) {}
if (new URLSearchParams(location.search).get('key')) history.replaceState(null, '', '/admin'); // the key stays in this browser; /admin reloads without it
if (!KEY) { KEY = window.prompt('Admin key') || ''; try { if (KEY) localStorage.setItem('dr.admin', KEY); } catch (e) {} }
function api(p, body) { return fetch(p, { method: body ? 'POST' : 'GET', headers: { 'Content-Type': 'application/json', 'X-Admin-Key': KEY }, body: body ? JSON.stringify(body) : undefined }).then(function (r) { return r.json(); }); }
var esc = function (s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]; }); };
var ct = function (ts) { return ts ? new Date(ts).toLocaleString('en-US', { timeZone: 'America/Chicago', month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }) : ''; };
@@ -12,7 +13,8 @@ function table(id, cols, rows) { var h = '<tr>' + cols.map(function (c) { return
var S = null;
function load() {
return api('/api/admin/state').then(function (d) {
if (d.error) { document.body.innerHTML = '<p style="padding:40px">' + esc(d.error) + '</p>'; return; }
if (d.error) { try { localStorage.removeItem('dr.admin'); } catch (e) {} document.body.innerHTML = '<p style="padding:40px">' + esc(d.error) + '. <a href="/admin">Enter the key again</a>.</p>'; return; }
$('stamp').textContent = 'updated ' + new Date().toLocaleTimeString('en-US', { timeZone: 'America/Chicago', hour: 'numeric', minute: '2-digit', second: '2-digit' }) + ' CT';
S = d; var p = d.pool;
$('links').innerHTML = 'Tester page: <a href="' + esc(d.curtain) + '">' + esc(d.curtain) + '</a> · POL $' + f4(d.usdPerPol) + ' (from the IAP contract)';
$('pool').innerHTML = stat('Available now', f4(p.available) + ' POL') + stat('In from refuels', f4(p.in)) + stat('Back from spends', f4(p.spends)) + stat('Out as drips', f4(p.out)) + stat('Seed float', f4(p.seed)) + stat('Granted today', f4(p.grantedToday) + ' / ' + f4(p.dailyCapPol));
@@ -37,5 +39,9 @@ $('settings').addEventListener('submit', function (e) {
api('/api/admin/settings', body).then(function (r) { $('saveOut').textContent = r.ok ? 'Saved.' : (r.error || 'Failed'); load(); });
});
$('tick').addEventListener('click', function () { $('tickOut').textContent = 'running…'; api('/api/admin/faucet/tick', {}).then(function (r) { $('tickOut').textContent = JSON.stringify(r); load(); }); });
$('refresh').addEventListener('click', function () { $('stamp').textContent = 'refreshing…'; load(); });
$('auto').addEventListener('change', function () { try { localStorage.setItem('dr.admin.auto', this.checked ? '1' : '0'); } catch (e) {} });
try { $('auto').checked = localStorage.getItem('dr.admin.auto') !== '0'; } catch (e) {}
setInterval(function () { if ($('auto').checked && !document.hidden && document.activeElement.tagName !== 'INPUT' && document.activeElement.tagName !== 'TEXTAREA') load(); }, 60000);
load();
})();
+1 -1
View File
@@ -144,7 +144,7 @@ const server = http.createServer(async (req, res) => {
// ---- static + pages ----
if (p === '/' || p === '/index.html') { if (curtained(req, res, u)) return; return sendFile(res, path.join(PUBLIC_DIR, 'index.html')); }
if (p === '/play') return sendFile(res, path.join(PUBLIC_DIR, 'play.html'));
if (p === '/admin') { if (!admin(req, u)) return page(res, 403, 'Admin', '<p>Key required.</p>'); return sendFile(res, path.join(PUBLIC_DIR, 'admin.html')); }
if (p === '/admin') return sendFile(res, path.join(PUBLIC_DIR, 'admin.html')); // the page asks for the key; every /api/admin call checks it
if (p === '/spawn.js') return sendFile(res, path.join(__dirname, 'lib', 'spawn.js'));
if (/^\/[a-z0-9._-]+\.(css|js|png|jpg|svg|ico|webp|mp4)$/i.test(p) && !p.includes('..')) return sendFile(res, path.join(PUBLIC_DIR, p.slice(1)));