From 9da67614f47b17fb3e6cbc050ffc6f5eb349a34b Mon Sep 17 00:00:00 2001 From: Nik K <4200435+Dorson@users.noreply.github.com> Date: Sat, 18 Apr 2020 22:59:16 +0200 Subject: [PATCH] improve logic against exception cases Improve automatic dark mode setting logic. + avoid conflict errors, when other color was already set. + fix conflict errors, when dark was already set as the CSS class. --- JS-CSS-Color-Settings-Recall-on-load.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/JS-CSS-Color-Settings-Recall-on-load.js b/JS-CSS-Color-Settings-Recall-on-load.js index 39c462a..16f2ab2 100644 --- a/JS-CSS-Color-Settings-Recall-on-load.js +++ b/JS-CSS-Color-Settings-Recall-on-load.js @@ -42,11 +42,15 @@ document.addEventListener("DOMContentLoaded", AfterDark() , true ) ; function AfterDark() { - var hour = new Date().getHours() ; + let hour = new Date().getHours() ; - if ( !localStorage.getItem( "ColorMode" ) && ( hour >= 20 || hour < 7 ) ) { + let ColorWasSet = localStorage.getItem( "ColorMode" ) ; - document.querySelector("html").classList.add( "dark" ) ; + let DarkWasSet = document.querySelector("html").classList.contains("dark") ; + + if ( ( hour >= 20 || hour < 7 ) && !( ColorWasSet || DarkWasSet ) ) + { + document.querySelector("html").classList.add( "dark" ) ; } }