Skip to content

Commit

Permalink
harden memory recall logic to be fail-safe
Browse files Browse the repository at this point in the history
Hardened the CSS class memory recall function against critical inpupt errors in exceptional cases.

One could never know if this code will run in the apps or hardware for critical life support.
Or maybe inside of machines that need to work 100% all the time to support safety of humans.


Now the functon exits the loop, if any errors of the input variables are found.
Limits possible attack vectors, where safety is an issue.


If you want to test the function for critical errors or with false data input,
use those console log variables for testing.

Fake the input data and see what happens in the console logs.


   console.log( "load state was: " + document.readyState ) ;

   console.log( "Tag : " + Tag ) ;

   console.log( "TagNotAlive : " + TagNotAlive ) ;

   console.log( "MemoryName : " + MemoryName ) ;

   console.log( "MemoryValue : " + MemoryValue ) ;
  • Loading branch information
Dorson authored Apr 22, 2020
1 parent 0cbfdf5 commit 3408781
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions JS-CSS-Class-Settings-Recall-on-load.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
-::: JavaScript CSS Class Memory Recall function for websites and HTML apps.
--- Original author :: Nik K. linkedin.com/in/nik--k---/ ( 4.Feb 2020).
--- 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
Expand All @@ -9,25 +9,42 @@
window.onerror = null ;


/* During load time, we remember the saved CSS classes from local memory */
/*
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() ;

});

document.addEventListener("DOMContentLoaded", RememberClasses() , true ) ;

function RememberClasses() {

var ClassMemory = [
const ClassMemory = [
["html" , "ColorMode"] ,
["html" , "TextSize"]
] ;

for ( let [ Tag, MemoryName ] of ClassMemory ) {
/* On Errors we skip one loop. */
if (!localStorage.getItem( MemoryName ) || !document.querySelector( Tag ) ) {

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 {

/* Else we remember and set CSS classes. */
var MemoryValue = localStorage.getItem( MemoryName );
else {

document.querySelector( Tag ).classList.add( MemoryValue );
}
}
Expand All @@ -36,6 +53,7 @@ function RememberClasses() {




/* In the dark, IF ColorMode was NOT set, and the hour is between 20:00 and 7:00 */

document.addEventListener("DOMContentLoaded", AfterDark() , true ) ;
Expand Down

0 comments on commit 3408781

Please sign in to comment.