Dark Mode Toggle

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Dark Mode Toggle
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Attiva la modalità scura in una pagina web con miglior contrasto
// @author       Magneto1
// @match        *://*/*
// @license      MIT
// @grant        GM.getValue
// @grant        GM.setValue
// @require      https://unpkg.com/[email protected]/darkreader.js
// ==/UserScript==

(async function() {
    'use strict';

    const namespace = 'dark-mode-toggle';
    const btn = document.createElement('button');

    // Crea il pulsante
    btn.textContent = '🌙';
    btn.id = namespace;
    btn.style.position = 'fixed';
    btn.style.bottom = '10px';
    btn.style.left = '10px';
    btn.style.zIndex = '10000';
    btn.style.fontSize = '20px';
    btn.style.opacity = '0.7';
    btn.style.border = 'none';
    btn.style.backgroundColor = 'transparent';
    btn.style.cursor = 'pointer';

    // Aggiungi il pulsante al corpo del documento
    document.body.appendChild(btn);

    // Funzione per attivare/disattivare la modalità scura
    const toggleDarkMode = async () => {
        if (await GM.getValue('darkModeEnabled')) {
            await GM.setValue('darkModeEnabled', false);
            DarkReader.disable();
            btn.textContent = '🌙'; // Icona per modalità chiara
        } else {
            await GM.setValue('darkModeEnabled', true);
            DarkReader.setFetchMethod(window.fetch);
            DarkReader.enable({
                brightness: 100,
                contrast: 90,
                sepia: 10
            });
            btn.textContent = '☀️'; // Icona per modalità scura
        }
    };

    // Imposta il comportamento del pulsante
    btn.onclick = toggleDarkMode;

    // Controlla se la modalità scura è già attivata
    try {
        if (await GM.getValue('darkModeEnabled')) {
            DarkReader.setFetchMethod(window.fetch);
            DarkReader.enable({
                brightness: 100,
                contrast: 90,
                sepia: 10
            });
            btn.textContent = '☀️'; // Icona per modalità scura
        }
    } catch (err) {
        console.warn('Errore nel recupero dello stato della modalità scura:', err);
    }
})();