Skip to content

Commit

Permalink
Remove comments to reduce size
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdHeat committed Aug 28, 2024
1 parent 833f6cc commit 687ae77
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,30 @@
<link rel="shortcut icon" href="{{ Configs.ctf_small_icon }}" type="image/x-icon">

<script>
"use strict";

/**
* Get user preferred theme from their past choice or browser
* @returns {String} User preferred theme
*/
// This script is inlined to reduce color flashing issues
function getPreferredTheme() {
const storedTheme = localStorage.getItem("theme");
if (storedTheme) {
return storedTheme;
}
// Firefox with 'resistFingerprint' activated always returns light
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}

/**
* Update navbar icon to match given theme.
* @param {String} theme - 'dark' or 'light'
*/
function showActiveTheme(theme) {
const activeThemeIcon = document.querySelector(".theme-switch i.fas");
activeThemeIcon.classList.toggle("fa-moon", theme === "dark");
activeThemeIcon.classList.toggle("fa-sun", theme !== "dark");
}

// Change body theme early to prevent flash
let currentTheme = getPreferredTheme();
document.documentElement.setAttribute("data-bs-theme", currentTheme);

// On browser color-scheme change, update
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
currentTheme = getPreferredTheme();
document.documentElement.setAttribute("data-bs-theme", currentTheme);
showActiveTheme(currentTheme);
});

window.addEventListener("load", () => {
showActiveTheme(currentTheme);

// On button click, switch
document.querySelectorAll(".theme-switch").forEach(e => {
e.addEventListener("click", ev => {
currentTheme = currentTheme === "light" ? "dark" : "light";
Expand Down

0 comments on commit 687ae77

Please sign in to comment.