1 | function updateTheme() { |
2 | if (localStorage.getItem("theme") == null) { |
3 | if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) { |
4 | console.log("Prefers dark"); |
5 | localStorage.setItem("theme", "dark") |
6 | } else { |
7 | localStorage.setItem("theme", "") |
8 | } |
9 | } |
10 | let themeName = localStorage.getItem("theme"); |
11 |
|
12 | document.querySelector("html").classList.value = themeName; |
13 | } |
14 |
|
15 | function toggleDarkMode() { |
16 | localStorage.setItem("theme", localStorage.getItem("theme") == "" ? "dark" : ""); |
17 | updateTheme(); |
18 | } |
19 |
|
20 | updateTheme(); |
21 | document.addEventListener('DOMContentLoaded', updateTheme); |
22 |
|