b84eed3be5
themehead.js is the one source of the tiny inline head script (and of AUTO_DEFAULT, the one constant Marty decides later: 0 = nothing stored keeps the page's current look, light for Control Room and public pages, dark for the sapphire pages; 1 = follow the device). The script sets data-theme on <html> from ls.theme (auto follows prefers-color-scheme and tracks changes live) and exposes window.LSTheme. tools/stamp-theme-head.cjs writes it into every static page before the first stylesheet with data-theme-base on <html>; blog, leaderboard, what's-new, the badge page, the unsubscribe page and the wall 404 read the module directly. The site CSP allows inline scripts by hash only, so themehead.CSP_HASH joins script-src (the script was silently blocked until then).
Account side: accounts.theme (JSON store and MySQL, alterSafe column), in the public account JSON and /api/me; POST /api/my/profile {theme} rides the existing profile save path and never touches the username. cr-shell.js renders a Light / Dark / Auto segmented control in the topbar of every Control Room screen and paints every .theme-seg on the page (the Profile pane gains an Appearance card with the same control); a click applies at once, saves locally and to the account when signed in; at sign-in the account value wins over the local one, and a local choice is pushed up when the account has none yet. view.html stops reading an undefined --bg so its light variant has a light stage. ?v= tags bumped on every changed stylesheet and script (20261101t / ls20261101t).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
34 lines
2.9 KiB
JavaScript
34 lines
2.9 KiB
JavaScript
// 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 <head>, 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 <html> 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 <html> 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 = '<script>' + BODY + '</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 <html> 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: '<!doctype html><html lang="en"' + themehead.htmlAttr('dark') + '><head>' + themehead.tag() + ...
|
|
module.exports = { AUTO_DEFAULT, SCRIPT, CSP_HASH, htmlAttr, tag };
|