Files
instantadpay/themehead.js
martbost 1aec9caf6c Theme switch: auto | light | dark, stored in localStorage iap.theme and on the account, applied before first paint on every page
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, dark everywhere on InstantAdPay; 1 = follow the device). It mirrors LinkSpin's themehead.js with only the storage key (iap.theme), the global (window.IAPTheme) and the event name (iap-theme) changed. The script sets data-theme on <html> from iap.theme (auto follows prefers-color-scheme and tracks changes live). tools/stamp-theme-head.cjs writes it into all 28 static site.css pages before the first stylesheet with data-theme-base="dark" on <html>; the blog, the 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 (without it the script is silently blocked).

Account side: accounts.theme (JSON store and MySQL, alterSafe theme VARCHAR(8)), in the public account JSON and /api/me; POST /api/my/profile {theme} rides the existing profile save path, never touches the username, and does not tap changed() (appearance is not mirrored to LinkSpin, same as LinkSpin's side). common.js owns the client side for every page: paints every .theme-seg, saves a click to the account, and IAP.themeSync lets the account value win over the local one at sign-in (a local choice is pushed up when the account has none yet); my.js calls it from render(). The Light / Dark / Auto segmented control sits in the member-area header (#boTheme) and in a Profile pane "Appearance" card, and in the admin header for the same session. shorts.html pins the dark ink set on its black video stage; view.html stops reading an undefined --bg. ?v= tags bumped on every changed stylesheet and script (20261101th): site.css on all 28 pages, common.js on 25, welcome.js on 17, support.js, my.js, engine-room.js, admin.js, admin-kits.js, blog-page.js.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-01 11:55:06 -06:00

35 lines
3.1 KiB
JavaScript

// Theme head script (2026-11-01). Marty approved light and dark themes for LinkSpin and said InstantAdPay gets the same
// treatment: 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). LinkSpin's themehead.js is the reference; only the storage key, the global and the event
// name differ (iap.theme / window.IAPTheme / "iap-theme").
//
// theme value: auto | light | dark, stored in localStorage "iap.theme"; signed-in members also keep it on the
// account (accounts.theme) and the account value wins at sign-in (common.js IAP.themeSync).
// 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.IAPTheme = { get, set, apply, base }.
//
// Nothing stored => the page keeps its current look: data-theme-base on <html> says what that is. Every InstantAdPay page
// is a site.css page, so the base is dark everywhere. 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")||"dark",'
+ 'm=window.matchMedia?matchMedia("(prefers-color-scheme: dark)"):{matches:false},v;'
+ 'function g(){try{v=localStorage.getItem("iap.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.IAPTheme={base:b,get:g,apply:ap,set:function(c,quiet){try{if(c==="light"||c==="dark"||c==="auto")localStorage.setItem("iap.theme",c);else localStorage.removeItem("iap.theme")}catch(e){}'
+ 'ap();if(!quiet)try{dispatchEvent(new CustomEvent("iap-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 === 'light' ? 'light' : 'dark') + '"'; }
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 };