novelupdates auto darkmode

override theme selection on native browser darkmode change. Basic Idea from Reddit Auto Dark Mode

  1. // ==UserScript==
  2. // https://greasyfork.org/scripts/445361-novelupdates-auto-darkmode
  3. // @name novelupdates auto darkmode
  4. // @namespace somethingthatshouldnotclashwithotherscripts
  5. // @version 1.0
  6. // @description override theme selection on native browser darkmode change. Basic Idea from Reddit Auto Dark Mode
  7. // @author SZ
  8. // @match www.novelupdates.com/*
  9. // @license Apache-2.0
  10. // @supportURL https://greasyfork.org/scripts/445361-novelupdates-auto-darkmode/feedback
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // ==/UserScript==
  14. /*jshint esversion: 6 */
  15.  
  16. let defaultDarkmodeIndex = 1;
  17. let isDarkModeActive = false;
  18. let darkModeTheme = defaultDarkmodeIndex;
  19. let standardTheme = 0;
  20. (function () {
  21. "use strict";
  22. const savedDarkModeValue = GM_getValue("darkModeTheme");
  23. const savedStandardModeValue = GM_getValue("standardTheme");
  24.  
  25. darkModeTheme = savedDarkModeValue ?? defaultDarkmodeIndex;
  26. standardTheme = savedStandardModeValue ?? standardTheme;
  27.  
  28. let selectField = document.getElementById("wi_themes");
  29. const setDarkMode = (isDarkMode) => {
  30. isDarkModeActive = isDarkMode;
  31. if (selectField) {
  32. //in case of wrong site (not working for forum theme toggle)
  33. //Admins of novelupdate forum could implement https://xenvn.com/threads/th-style-switch-xenforo-2.329/
  34. let currentStyleIndex = selectField.selectedIndex;
  35. let currentStyleName = document.getElementById("wi_themes").options[
  36. currentStyleIndex
  37. ].value;
  38.  
  39. if (isDarkMode) {
  40. if (currentStyleIndex != darkModeTheme)
  41. selectField.selectedIndex = darkModeTheme;
  42. } else {
  43. if (currentStyleIndex != standardTheme)
  44. selectField.selectedIndex = standardTheme;
  45. }
  46.  
  47. selectField.dispatchEvent(new Event("change", { bubbles: true }));
  48. }
  49. };
  50.  
  51. const updateDarkMode = (e) => {
  52. setDarkMode(e.matches);
  53. };
  54. if (window.matchMedia) {
  55. // if the browser/os supports system-level color scheme
  56. const darkModeSetting = window.matchMedia("(prefers-color-scheme: dark)");
  57. setDarkMode(darkModeSetting.matches);
  58. darkModeSetting.addEventListener("change", updateDarkMode);
  59. } else {
  60. // otherwise use local time to decide
  61. let hour = new Date().getHours();
  62. setDarkMode(hour > 18 || hour < 8);
  63. }
  64.  
  65. const setSelectedTheme = (selectedIndex) => {
  66. let currentStyleIndex = selectField.selectedIndex;
  67. if (isDarkModeActive) {
  68. darkModeTheme = currentStyleIndex;
  69. GM_setValue("darkModeTheme", darkModeTheme);
  70. } else {
  71. standardTheme = currentStyleIndex;
  72. GM_setValue("standardTheme", standardTheme);
  73. }
  74. };
  75.  
  76. const changeSelectedTheme = (e) => {
  77. setSelectedTheme(e.matches);
  78. };
  79.  
  80. selectField?.addEventListener("change", changeSelectedTheme);
  81. window.removeEventListener("beforeunload", changeSelectedTheme);
  82. window.removeEventListener("beforeunload", updateDarkMode);
  83. })();