SHARE
TWEET

Untitled

a guest Feb 16th, 2020 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Utils based on http://youmightnotneedjquery.com/ */
  2. /* JQuery ready */
  3. function ready(fn) {
  4.   if (document.readyState != 'loading'){
  5.     fn();
  6.   } else if (document.addEventListener) {
  7.     document.addEventListener('DOMContentLoaded', fn);
  8.   } else {
  9.     document.attachEvent('onreadystatechange', function() {
  10.       if (document.readyState != 'loading')
  11.         fn();
  12.     });
  13.   }
  14. }
  15.  
  16. /* JQuery addClass */
  17. function addClass (el, className) {
  18.   if (el.classList) {
  19.     el.classList.add(className);
  20.   } else {
  21.     var current = el.className, found = false;
  22.     var all = current.split(' ');
  23.     for(var i=0; i<all.length, !found; i++) found = all[i] === className;
  24.     if(!found) {
  25.       if(current === '') el.className = className;
  26.       else el.className += ' ' + className;
  27.     }
  28.   }
  29. }
  30.  
  31. /* JQuery removeClass */
  32. function removeClass (el, className) {
  33.   if (el.classList)
  34.     el.classList.remove(className);
  35.   else
  36.     el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
  37. }
  38.  
  39. /* JQuery hasClass */
  40. function hasClass (el, className) {
  41.   if (el.classList)
  42.     return el.classList.contains(className);
  43.   else
  44.     return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
  45. }
  46.  
  47. /* JQuery on */
  48. function addEventListener(el, eventName, handler) {
  49.   if (el.addEventListener) {
  50.     el.addEventListener(eventName, handler);
  51.   } else {
  52.     el.attachEvent('on' + eventName, function(){
  53.       handler.call(el);
  54.     });
  55.   }
  56. }
  57. /* Utils end */
  58.  
  59. /* Script start here */
  60. ready(function () {
  61.   const wrapper = document.getElementById('wrapper')
  62.   const toggler = document.getElementById('theme-toggler')
  63.  
  64.   if (localStorage.theme) {
  65.     addClass(wrapper, localStorage.theme)
  66.   } else {
  67.     addClass(wrapper, 'light-mode')
  68.   }
  69.  
  70.   addEventListener(toggler, 'click', function () {
  71.     if (hasClass(wrapper, 'light-mode')) {
  72.       removeClass(wrapper, 'light-mode')
  73.       addClass(wrapper, 'night-mode')
  74.  
  75.       localStorage.theme = 'night-mode'
  76.     } else {
  77.       removeClass(wrapper, 'night-mode')
  78.       addClass(wrapper, 'light-mode')
  79.  
  80.       localStorage.theme = 'light-mode'
  81.     }
  82.   })
  83. })
  84. /* End */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top