-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
JS-CSS-Class-Settings-Recall-on-load.js
74 lines (45 loc) · 1.69 KB
/
JS-CSS-Class-Settings-Recall-on-load.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
-::: JavaScript CSS Class Memory Recall function for websites and HTML apps.
--- Original author :: Nik K. linkedin.com/in/nik--k---/ ( 4.Feb 2020).
--- Code adoption :: YourName (date).
--- License :: GNU General Public License v 3.0.
--- Latest version :: github.com/Dorson/CSS-Dark-Mode-and-color-switch
*/
window.onerror = null ;
/*
During load time, we remember the saved CSS classes from localStorage
Only works with HTML Tags that are already loaded before the script run !
Optimally the HTML tag. Works for the first Tag of it's type only !
*/
document.addEventListener( 'DOMContentLoaded' , function() {
RememberClasses() ;
});
function RememberClasses() {
const ClassMemory = [
["html" , "ColorMode"] ,
["html" , "TextSize"]
] ;
for ( let [ Tag , MemoryName ] of ClassMemory ) {
let TagNotAlive = !(document.querySelector( Tag )) ;
let MemoryValue = window.localStorage.getItem( MemoryName ) ;
/* On logic Errors we skip one loop. */
if ( !MemoryValue || TagNotAlive ) {
continue ;
}
/* Else we remember and set CSS classes. */
else {
document.querySelector( Tag ).classList.add( MemoryValue );
}
}
}
/* In the dark, IF ColorMode was NOT set, and the hour is between 20:00 and 7:00 */
document.addEventListener("DOMContentLoaded", AfterDark() , true ) ;
function AfterDark() {
let hour = new Date().getHours() ;
let ColorWasSet = localStorage.getItem( "ColorMode" ) ;
let DarkWasSet = document.querySelector("html").classList.contains("dark") ;
if ( ( hour >= 20 || hour < 7 ) && !( ColorWasSet || DarkWasSet ) )
{
document.querySelector("html").classList.add( "dark" ) ;
}
}