Dark Mode Toggle

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
    }
})();