lang.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var currentTheme = ( document.cookie.split('; ').find( cookie => {
  2. return cookie.split('=')[0] === 'theme' && /^"(?:light|dark)"$/.test(( cookie.split('=')[1] || '' ));
  3. } ) || 'dark' ).replace( /^theme="(light|dark)"$/, '$1' );
  4. var lightTheme = document.getElementById('theme-light');
  5. var darkTheme = document.getElementById('theme-dark');
  6. lightTheme.onclick = function() {
  7. document.cookie = 'theme="light"; Path=/; Max-Age=31536000';
  8. document.documentElement.classList.add('theme-light');
  9. lightTheme.setAttribute('style', 'display: none;');
  10. darkTheme.removeAttribute('style');
  11. };
  12. darkTheme.onclick = function() {
  13. document.cookie = 'theme="dark"; Path=/; Max-Age=31536000';
  14. document.documentElement.classList.remove('theme-light');
  15. darkTheme.setAttribute('style', 'display: none;');
  16. lightTheme.removeAttribute('style');
  17. };
  18. document.getElementById('theme-separator').removeAttribute('style');
  19. if ( currentTheme === 'light' ) {
  20. darkTheme.removeAttribute('style');
  21. document.documentElement.classList.add('theme-light');
  22. }
  23. else {
  24. lightTheme.removeAttribute('style');
  25. document.documentElement.classList.remove('theme-light');
  26. }
  27. var channellist = document.getElementById('channellist');
  28. var langSelector = document.createElement('div');
  29. langSelector.id = 'lang-selector';
  30. langSelector.textContent = selectLanguage;
  31. var langIcon = document.createElement('img');
  32. langIcon.setAttribute('src', '/src/language.svg');
  33. langSelector.prepend(langIcon);
  34. var langDropdown = document.createElement('div');
  35. langDropdown.id = 'lang-dropdown';
  36. langDropdown.setAttribute('style', `max-height: ${window.innerHeight - 80}px;`);
  37. var langOptions = Object.keys(allLangs).map( function(lang) {
  38. var langOption = document.createElement('div');
  39. langOption.textContent = allLangs[lang];
  40. if ( document.documentElement.lang === lang ) langOption.className = 'current';
  41. langOption.onclick = function() {
  42. document.cookie = `language="${lang}"; Path=/; Max-Age=31536000`;
  43. location.reload();
  44. };
  45. return langOption;
  46. } );
  47. langDropdown.append(...langOptions);
  48. langSelector.append(langDropdown);
  49. channellist.after(langSelector);
  50. channellist.setAttribute('style', 'bottom: 32px;');