2019-10-24 13:50:51 +02:00
|
|
|
{
|
|
|
|
var $darkmode = document.querySelector(".enter_darkmode");
|
|
|
|
const init = () => {
|
|
|
|
$darkmode.addEventListener("click", toggleDarkmode);
|
|
|
|
enableDarkmode();
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleDarkmode = () => {
|
2019-10-24 16:15:35 +02:00
|
|
|
localStorage.setItem("darkmodeChanged", 'true');
|
2019-10-24 13:50:51 +02:00
|
|
|
var newState = !(localStorage.getItem("darkmode") == 'true');
|
|
|
|
localStorage.setItem("darkmode", newState);
|
|
|
|
enableDarkmode();
|
|
|
|
}
|
|
|
|
const enableDarkmode = () => {
|
|
|
|
if (typeof(Storage) !== "undefined") {
|
2019-10-24 16:15:35 +02:00
|
|
|
if(localStorage.getItem("darkmode") == 'true' && localStorage.getItem("darkmodeChanged") == 'true' || (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && localStorage.getItem("darkmodeChanged") != 'true')){
|
|
|
|
document.querySelector('body').classList.remove('lightmode');
|
|
|
|
document.querySelector('body').classList.add('darkmode');
|
2019-10-24 13:50:51 +02:00
|
|
|
} else {
|
|
|
|
document.querySelector('body').classList.remove('darkmode');
|
|
|
|
document.querySelector('body').classList.add('lightmode');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('You browser does not support local storage, no darkmode for you!' )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|