all repos — h3rald @ d3dbf11d8322a2fe2789df6d2f21c2dec41e8fd2

The sources of https://h3rald.com

assets/js/scripts.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
(function (window) {
  'use strict';

  // Theme Switching
  var themeSwitcher = document.getElementById('theme-switcher');
  var themeLinks = document.getElementsByClassName('theme-css-link');
  var themeNames = {
    light: 'day',
    dark: 'night'
  }
  var hour = new Date().getHours();
  var currentTheme = themeLinks[0].href.match(/(light|dark)/)[1];
  var actualTheme = (hour >= 18 || hour < 6) ? 'dark' : 'light';

  function switchTheme() {
    var newTheme = currentTheme === 'light' ? 'dark' : 'light';
    for (var i = 0; i < themeLinks.length; i++) {
      themeLinks[i].href = themeLinks[i].href.replace(currentTheme, newTheme);
    }
    themeSwitcher.title = themeSwitcher.title.replace(themeNames[newTheme], themeNames[currentTheme]);
    themeSwitcher.innerText = themeNames[currentTheme];
    actualTheme = newTheme;
    currentTheme = newTheme;
  }

  themeSwitcher.addEventListener('click', switchTheme);
  if (actualTheme !== currentTheme) {
    switchTheme();
    currentTheme = actualTheme;
  }

})(window)