This repository has been archived on 2024-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
hectabit-website/darkmode.js

76 lines
2.3 KiB
JavaScript

var iframe = document.getElementById('iframe');
// Check if a dark mode cookie exists, and apply dark mode if it does
if (getCookie("darkMode") === "true") {
applyDarkMode();
}
// Detect dark mode
function isDarkModeEnabled() {
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
}
if (isDarkModeEnabled()) {
applyDarkMode();
} else {
console.log('Dark mode is not enabled.');
}
function toggleDarkMode() {
if (getCookie("darkMode") === "true") {
// Disable dark mode
setCookie("darkMode", "false", 365, "Strict");
applyLightMode();
} else {
// Enable dark mode
setCookie("darkMode", "true", 365, "Strict");
applyDarkMode();
}
}
function applyDarkMode() {
document.body.classList.add("dark-mode");
iframe.onload = function() {
// The iframe has loaded, now you can access its content
iframe.contentWindow.document.body.classList.add("dark-mode");
};
iframe.contentWindow.document.body.classList.add("dark-mode");
}
function applyLightMode() {
document.body.classList.remove("dark-mode");
iframe.onload = function() {
// The iframe has loaded, now you can access its content
iframe.contentWindow.document.body.classList.remove("dark-mode");
};
iframe.contentWindow.document.body.classList.remove("dark-mode");
}
// Helper functions for handling cookies
function setCookie(name, value, days, sameSite) {
var expires = "";
var sameSiteAttribute = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
if (sameSite) {
sameSiteAttribute = "; samesite=" + sameSite;
}
document.cookie = name + "=" + (value || "") + expires + "; path=/" + sameSiteAttribute;
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}