( function() { 'use strict'; if ( 'getElementById' in document && 'addEventListener' in window ) { //Create the cookie object const cookieStorage = { setCookie: function setCookie(key, value, time, path) { var expires = new Date(); expires.setTime(expires.getTime() + time); var pathValue = ''; if (typeof path !== 'undefined') { pathValue = 'path=' + path + ';'; } document.cookie = key + '=' + value + ';' + pathValue + 'expires=' + expires.toUTCString(); }, getCookie: function getCookie(key) { var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); return keyValue ? keyValue[2] : null; }, removeCookie: function removeCookie(key) { document.cookie = key + '=; Max-Age=0; path=/'; } }; //Click on dark mode icon. Add dark mode classes and wrappers. Store user preference through sessions const swichi = document.getElementById( 'switch' ); const bode = document.getElementById( 'site-body' ); swichi.addEventListener( 'click', ()=> { //show either moon or sun swichi.classList.toggle( 'activu' ); //if dark mode is selected if ( swichi.classList.contains( 'activu' ) ) { //add dark mode into body bode.classList.add( 'dark-mode' ); cookieStorage.setCookie('tsNightMode', 'true', 2628000000, '/'); } else { bode.classList.remove( 'dark-mode' ); setTimeout( ()=> { cookieStorage.removeCookie('tsNightMode'); }, 100); } }); swichi.addEventListener( 'keypress', (evt) => { if ( evt.key === 'Enter' ) { evt.preventDefault(); bode.classList.add( 'dark-mode'); } }); //Check Storage. Display user preference if (cookieStorage.getCookie('tsNightMode')) { bode.classList.add( 'dark-mode' ); swichi.classList.add( 'activu' ); } } }) ();