Dark Mode Toggle

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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