Blooket Dark Mode

Dark mode for Blooket (yay xander is no longer metalic)

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Blooket Dark Mode
// @namespace    http://tampermonkey.net/
// @version      1.4
// @license      MIT
// @description  Dark mode for Blooket (yay xander is no longer metalic)
// @author       generic
// @match        https://*.blooket.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Function to change colors of non-text elements (excluding images)
    function changeColors() {
        const elements = document.querySelectorAll('*:not(img):not(svg):not(path):not(script):not(style):not([data-original-color])');

        elements.forEach(element => {
            const computedStyle = getComputedStyle(element);
            const backgroundColor = computedStyle.backgroundColor;
            const textColor = computedStyle.color;

            if (backgroundColor) {
                // Calculate luminance for background color
                const bgLuminance = calculateLuminance(backgroundColor);
                if (bgLuminance > calculateLuminance('rgb(195, 195, 195)')) {
                    element.style.backgroundColor = '#161616';
                    // Store the original background color to prevent repeated changes
                    element.setAttribute('data-original-color', backgroundColor);
                }
            }

            if (textColor) {
                // Change text color to white
                element.style.color = '#fcfcfc';
            }
        });
    }

    // Function to calculate luminance from RGB values
    function calculateLuminance(rgb) {
        const parts = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
        if (parts) {
            const r = parseInt(parts[1]) / 255;
            const g = parseInt(parts[2]) / 255;
            const b = parseInt(parts[3]) / 255;
            return 0.2126 * r + 0.7152 * g + 0.0722 * b;
        }
        return 0;
    }

    // Function to periodically check and change colors
    function periodicallyCheckColors() {
        setInterval(changeColors, 1); // Check every 0.00001 seconds (10 milliseconds)
    }

    // Function to apply color change when navigating within the site
    function applyColorChangeOnNavigation() {
        const observer = new MutationObserver(changeColors);
        const config = { childList: true, subtree: true };

        observer.observe(document.body, config);
    }

    // Run the color-changing function when the page loads
    window.addEventListener('load', () => {
        changeColors();
        applyColorChangeOnNavigation();
        periodicallyCheckColors();
    });
})();