Dark Mode Toggle

Attiva la modalità scura in una pagina web con miglior contrasto

  1. // ==UserScript==
  2. // @name Dark Mode Toggle
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Attiva la modalità scura in una pagina web con miglior contrasto
  6. // @author Magneto1
  7. // @match *://*/*
  8. // @license MIT
  9. // @grant GM.getValue
  10. // @grant GM.setValue
  11. // @require https://unpkg.com/darkreader@4.9.58/darkreader.js
  12. // ==/UserScript==
  13.  
  14. (async function() {
  15. 'use strict';
  16.  
  17. const namespace = 'dark-mode-toggle';
  18. const btn = document.createElement('button');
  19.  
  20. // Crea il pulsante
  21. btn.textContent = '🌙';
  22. btn.id = namespace;
  23. btn.style.position = 'fixed';
  24. btn.style.bottom = '10px';
  25. btn.style.left = '10px';
  26. btn.style.zIndex = '10000';
  27. btn.style.fontSize = '20px';
  28. btn.style.opacity = '0.7';
  29. btn.style.border = 'none';
  30. btn.style.backgroundColor = 'transparent';
  31. btn.style.cursor = 'pointer';
  32.  
  33. // Aggiungi il pulsante al corpo del documento
  34. document.body.appendChild(btn);
  35.  
  36. // Funzione per attivare/disattivare la modalità scura
  37. const toggleDarkMode = async () => {
  38. if (await GM.getValue('darkModeEnabled')) {
  39. await GM.setValue('darkModeEnabled', false);
  40. DarkReader.disable();
  41. btn.textContent = '🌙'; // Icona per modalità chiara
  42. } else {
  43. await GM.setValue('darkModeEnabled', true);
  44. DarkReader.setFetchMethod(window.fetch);
  45. DarkReader.enable({
  46. brightness: 100,
  47. contrast: 90,
  48. sepia: 10
  49. });
  50. btn.textContent = '☀️'; // Icona per modalità scura
  51. }
  52. };
  53.  
  54. // Imposta il comportamento del pulsante
  55. btn.onclick = toggleDarkMode;
  56.  
  57. // Controlla se la modalità scura è già attivata
  58. try {
  59. if (await GM.getValue('darkModeEnabled')) {
  60. DarkReader.setFetchMethod(window.fetch);
  61. DarkReader.enable({
  62. brightness: 100,
  63. contrast: 90,
  64. sepia: 10
  65. });
  66. btn.textContent = '☀️'; // Icona per modalità scura
  67. }
  68. } catch (err) {
  69. console.warn('Errore nel recupero dello stato della modalità scura:', err);
  70. }
  71. })();