hugo-theme-noteworthy/static/js/main.js

45 lines
1019 B
JavaScript
Raw Normal View History

2024-11-29 13:56:18 +00:00
document.addEventListener("DOMContentLoaded", function() {
setDarkMode()
})
function setCookie(cname, cvalue, exdays) {
document.cookie = cname + "=" + cvalue + ";path=/";
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function isStringTrue(val) {
return String(val).toLowerCase() === 'true'
}
function toggleDarkMode() {
let darkMode = getCookie("setDarkMode");
if(isStringTrue(darkMode))
setCookie("setDarkMode", false);
else
setCookie("setDarkMode", true);
document.body.classList.toggle("dark-mode");
}
function setDarkMode() {
let darkMode = getCookie("setDarkMode");
if(isStringTrue(darkMode))
document.body.classList.toggle("dark-mode");
}