Skip to main content

BOOT_JS

Constant BOOT_JS 

Source
pub const BOOT_JS: &str = "// Laterite colour mode: light, dark, or auto.\n//\n// Inline this before any stylesheet. It runs blocking, on purpose: a deferred\n// script runs after first paint, so a dark-mode reader would see a white flash.\n//\n// The contract it establishes, which is all a surface needs to know:\n//\n//   <html data-theme=\"light\">  the resolved mode; style against this\n//   localStorage[\"lat-mode\"]   the reader\'s choice: light | dark | auto\n//   window.latApplyMode(mode)  resolve and apply a mode now\n//\n// \"auto\" means follow the operating system, including when the system changes\n// while the page is open: a page left open through dusk follows along instead of\n// waiting for a reload. An explicit light or dark is the reader overriding the\n// system and is left alone.\n(function () {\n  var mq = matchMedia(\'(prefers-color-scheme:dark)\');\n  window.latApplyMode = function (m) {\n    var dark = m === \'dark\' || (m === \'auto\' && mq.matches);\n    document.documentElement.setAttribute(\'data-theme\', dark ? \'dark\' : \'light\');\n  };\n  var current = function () {\n    try { return localStorage.getItem(\'lat-mode\') || \'auto\'; } catch (e) { return \'auto\'; }\n  };\n  window.latSetMode = function (m) {\n    try { localStorage.setItem(\'lat-mode\', m); } catch (e) {}\n    window.latApplyMode(m);\n  };\n  window.latMode = current;\n  try { window.latApplyMode(current()); } catch (e) {}\n  var onChange = function () {\n    if (current() === \'auto\') window.latApplyMode(\'auto\');\n  };\n  if (mq.addEventListener) {\n    mq.addEventListener(\'change\', onChange);\n  } else if (mq.addListener) {\n    // Safari before 14 has only the deprecated form.\n    mq.addListener(onChange);\n  }\n})();\n";
Expand description

The boot script’s JavaScript, without a surrounding <script> element.

Serve it as a file when a Content Security Policy forbids inline script, or embed it yourself with a nonce. Otherwise prefer boot_markup.