Dark Mode Toggle (Mobile Manual)

Enable dark mode with a toggle button

  1. // ==UserScript==
  2. // @name Dark Mode Toggle (Mobile Manual)
  3. // @version 0.0.4
  4. // @description Enable dark mode with a toggle button
  5. // @namespace https://greasyfork.org/en/users/28298
  6. // @author https://greasyfork.org/en/users/28298
  7. // @homepage https://greasyfork.org/en/scripts/523876
  8. // @license MIT
  9. // @match *://*/*
  10. // @grant none
  11. // ==/UserScript==
  12. // original author: Michael Wang https://greasyfork.org/en/scripts/472251-dark-mode/code
  13. // with help of claude ai
  14. (function () {
  15. // Create style element for dark mode
  16. const darkStyle = document.createElement('style');
  17. darkStyle.textContent = `
  18. html {
  19. filter: invert(1) hue-rotate(180deg) contrast(0.8);
  20. }
  21. /** reverse filter for media elements */
  22. img, video, picture, canvas, iframe, embed {
  23. filter: invert(1) hue-rotate(180deg);
  24. }
  25. `;
  26. // Initialize based on page's current state
  27. let darkMode = false; // Start with no filter
  28. const pageIsDark = document.documentElement.classList.contains('dark');
  29. // Create toggle button
  30. const button = document.createElement('button');
  31. button.innerHTML = pageIsDark ? '☼' : '☽';
  32. button.style.position = 'fixed';
  33. button.style.bottom = '10px';
  34. button.style.left = '15px';
  35. button.style.zIndex = '1000';
  36. button.style.background = 'none';
  37. button.style.border = 'none';
  38. button.style.fontSize = '24px';
  39. button.style.cursor = 'pointer';
  40. button.style.color = 'inherit';
  41. // Toggle dark mode on button click
  42. button.addEventListener('click', () => {
  43. darkMode = !darkMode;
  44. if (darkMode) {
  45. document.head.appendChild(darkStyle);
  46. button.innerHTML = pageIsDark ? '☽' : '☼';
  47. } else {
  48. document.head.removeChild(darkStyle);
  49. button.innerHTML = pageIsDark ? '☼' : '☽';
  50. }
  51. });
  52. document.body.appendChild(button);
  53. })();