OXYGENHUB
◆ v0.1.0 spectra / crates / radar / assets / radar.js
897 B · JavaScript · last changed Radar: Live, channels, threads and the log for the owner, signed in by a CLI-signed link; ledger writes survive a cancelled caller (#10) 2 days ago source blame history raw
1
// Radar's one script (spec §17.3): when the event stream says something
2
// changed, fetch this page again and replace <main>. Pages work without it.
3
(function () {
4
  var main = document.querySelector("main[data-live]");
5
  if (!main || !window.EventSource) return;
6
  var timer = null;
7
  function refresh() {
8
    fetch(location.href, { credentials: "same-origin" }).then(function (r) { return r.ok ? r.text() : null; }).then(function (html) {
9
      if (!html) return;
10
      var next = new DOMParser().parseFromString(html, "text/html").querySelector("main");
11
      var active = document.activeElement;
12
      if (next && !(active && (active.tagName === "TEXTAREA" || active.tagName === "INPUT") && active.value)) main.innerHTML = next.innerHTML;
13
    });
14
  }
15
  var es = new EventSource("/radar/events");
16
  es.onmessage = function () { clearTimeout(timer); timer = setTimeout(refresh, 300); };
17
})();