navid/veinsforge / 2d62c8d19eba4bd39b47efbdda14ec6b3e98e2d648301bc60535ff297b0d873b / internal/web/static/app.js

app.js

← internal/web/static · 1481 bytes · raw · download

(function () {
  "use strict";

  var KEY = "veinsforge-theme";
  var root = document.documentElement;

  function preferred() {
    try {
      if (window.matchMedia && window.matchMedia("(prefers-color-scheme: light)").matches) {
        return "light";
      }
    } catch (e) {}
    return "dark";
  }

  function apply(theme) {
    root.setAttribute("data-theme", theme === "light" ? "light" : "dark");
  }

  try {
    apply(localStorage.getItem(KEY) || preferred());
  } catch (e) {
    apply(preferred());
  }

  var toggle = document.getElementById("theme-toggle");
  if (toggle) {
    toggle.addEventListener("click", function () {
      var next = root.getAttribute("data-theme") === "light" ? "dark" : "light";
      apply(next);
      try {
        localStorage.setItem(KEY, next);
      } catch (e) {}
    });
  }

  var copies = document.querySelectorAll(".copy-input");
  for (var i = 0; i < copies.length; i++) {
    copies[i].addEventListener("focus", function () {
      this.select();
    });
  }

  function highlightCode() {
    if (!window.hljs) return;
    var blocks = document.querySelectorAll("pre.code code[class*='language-']");
    for (var i = 0; i < blocks.length; i++) {
      var block = blocks[i];
      // Very large files can lock up the tab during synchronous highlighting.
      if (block.textContent.length > 500000) continue;
      try {
        window.hljs.highlightElement(block);
      } catch (e) {}
    }
  }

  highlightCode();
})();