-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
32 lines (28 loc) · 1.01 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
if(document.readyState == 'loading'){
document.addEventListener('DOMContentLoaded', detectColorScheme);
}
else{
detectColorScheme();
}
//As seen on
//https://stackoverflow.com/questions/56300132/how-to-override-css-prefers-color-scheme-setting
//2nd part in body needs to be after the checkbuton+label!!!
function detectColorScheme(){
var theme="light"; //default to light
//local storage is used to override OS theme settings
if(localStorage.getItem("theme")){
if(localStorage.getItem("theme") == "dark"){
theme = "dark";
}
} else if(!window.matchMedia) {
//matchMedia method not supported
return false;
} else if(window.matchMedia("(prefers-color-scheme: dark)").matches) {
//OS theme setting detected as dark
theme = "dark";
}
//dark theme preferred, set document with a `data-theme` attribute
if (theme=="dark") {
document.documentElement.setAttribute("data-theme", "dark");
}
}