Dark Mode Toggle

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);
    }
})();