// Theme head script (2026-11-01). A member asked for light and dark themes: some readers are vision impaired, some // struggle with bright screens, some with dark ones. This is the ONE source of the tiny inline script every page // carries in its , before any stylesheet, so the first paint is already the chosen theme (no flash). // // theme value: auto | light | dark, stored in localStorage "ls.theme"; signed-in members also keep it on the // account (accounts.theme) and the account value wins at sign-in (cr-shell.js). // data-theme on is always resolved to light or dark (auto follows prefers-color-scheme and tracks changes). // data-theme-choice carries the raw choice for the switch UI. window.LSTheme = { get, set, apply, base }. // // Nothing stored => the page keeps its current look: data-theme-base on says what that is (light for the // Control Room and public pages, dark for the sapphire site.css pages). AUTO_DEFAULT below is the one constant // Marty decides later: set it to 1 and pages with nothing stored follow the device instead. After changing it, // run `node tools/stamp-theme-head.cjs` so every static page carries the new copy (server-rendered pages read // this module directly). const AUTO_DEFAULT = 0; const BODY = '(function(){var A=' + AUTO_DEFAULT + ',h=document.documentElement,b=h.getAttribute("data-theme-base")||"light",' + 'm=window.matchMedia?matchMedia("(prefers-color-scheme: dark)"):{matches:false},v;' + 'function g(){try{v=localStorage.getItem("ls.theme")}catch(e){v=null}return v==="light"||v==="dark"||v==="auto"?v:(A?"auto":b)}' + 'function ap(){var c=g();h.setAttribute("data-theme",c==="auto"?(m.matches?"dark":"light"):c);h.setAttribute("data-theme-choice",c)}' + 'ap();if(m.addEventListener)m.addEventListener("change",ap);else if(m.addListener)m.addListener(ap);' + 'window.LSTheme={base:b,get:g,apply:ap,set:function(c,quiet){try{if(c==="light"||c==="dark"||c==="auto")localStorage.setItem("ls.theme",c);else localStorage.removeItem("ls.theme")}catch(e){}' + 'ap();if(!quiet)try{dispatchEvent(new CustomEvent("ls-theme",{detail:g()}))}catch(e){}}}})()'; const SCRIPT = ''; // The site CSP allows inline scripts by hash only (server.js CSP): one source, one hash. Changing BODY changes the hash, // and the static pages must be re-stamped so they carry the same bytes. const CSP_HASH = "'sha256-" + require('crypto').createHash('sha256').update(BODY).digest('base64') + "'"; // base: 'light' | 'dark' (the page's look when nothing is stored). Returns the attribute for the tag and the script. function htmlAttr(base) { return ' data-theme-base="' + (base === 'dark' ? 'dark' : 'light') + '"'; } function tag() { return SCRIPT; } // whole-head helper for server-rendered pages: '' + themehead.tag() + ... module.exports = { AUTO_DEFAULT, SCRIPT, CSP_HASH, htmlAttr, tag };