Dark/Light Mode Toggle

Toggle between dark and light mode on any website

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Dark/Light Mode Toggle
// @namespace    http://your.namespace.com
// @version      1.2
// @description  Toggle between dark and light mode on any website
// @author       Your Name
// @license      MIT
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const toggleButton = document.createElement('button');
    toggleButton.textContent = 'Toggle Dark/Light Mode';
    toggleButton.style.position = 'fixed';
    toggleButton.style.top = '20px';
    toggleButton.style.right = '20px';
    document.body.appendChild(toggleButton);

    let darkModeEnabled = localStorage.getItem('darkModeEnabled') === 'true';
    setMode(darkModeEnabled);

    toggleButton.addEventListener('click', () => {
        darkModeEnabled = !darkModeEnabled;
        setMode(darkModeEnabled);
        localStorage.setItem('darkModeEnabled', darkModeEnabled);
    });

    function setMode(isDarkMode) {
        if (isDarkMode) {
            document.documentElement.style.backgroundColor = '#1a1a1a';
            document.documentElement.style.color = '#ffffff';
        } else {
            document.documentElement.style.backgroundColor = '#ffffff';
            document.documentElement.style.color = '#000000';
        }
    }
})();