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

59 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

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