// 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 , 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 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 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 = ''; // 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 === 'light' ? 'light' : 'dark') + '"'; } function tag() { return SCRIPT; } // whole-head helper for server-rendered pages: '' + themehead.tag() + ... module.exports = { AUTO_DEFAULT, SCRIPT, CSP_HASH, htmlAttr, tag };