OXYGENHUB
# f2d2776b2a14 oxygenhub / assets / backdrop-boot.js
1.6 KiB · JavaScript · last changed No inline code in pages, so the CSP can be enforced (#227) 18 days ago source blame history raw
a7aa76ac Andy McCall 1
/* Starts the page's backdrop (docs/backgrounds.md); a module of its own for the CSP (#227). The assets' version comes from this file's own address. */
2
const v = new URL(import.meta.url).search;
3
const canvas = document.getElementById("backdrop");
4
const chosen = document.cookie.match(/(?:^|; )backdrop=(moving|still|off)/);
5
const touch = matchMedia("(pointer: coarse)").matches && !matchMedia("(pointer: fine)").matches;
6
const pick = chosen ? chosen[1] : (touch || (navigator.connection && navigator.connection.saveData) ? "still" : "moving");
7
if (pick === "off") {
8
  canvas.remove();
9
} else {
10
  try {
11
    const bd = await import(`/static/backdrop/backdrop.js${v}`);
12
    await bd.default({ module_or_path: `/static/backdrop/backdrop_bg.wasm${v}` });
13
    const data = JSON.parse(document.getElementById("backdrop-data").textContent);
14
    bd.start(canvas, data, pick === "still");
15
    // Live (§5): a sky that names its repositories (`mine` or `hub`) asks
16
    // every 30 s, while the page is shown, what happened since.
17
    if (data.live) {
18
      let since = data.now;
19
      const poll = async () => {
20
        if (document.hidden) return;
21
        try {
22
          const r = await fetch(`/sky/arrivals?scene=${data.live}&since=${since}`, { headers: { accept: "application/json" } });
23
          if (!r.ok) return;
24
          const got = await r.json();
25
          since = got.now;
26
          if (got.events.length) bd.arrive(got);
27
        } catch (e) {}
28
      };
29
      setInterval(poll, 30000);
30
      document.addEventListener("visibilitychange", poll);
31
    }
32
  } catch (e) {
33
    canvas.remove();
34
  }
35
}