Hiding Minesweeper

优雅扫雷

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Hiding Minesweeper
// @namespace    https://minesweeper.cn/
// @version      1.2
// @description  优雅扫雷
// @author       977
// @match        https://minesweeper.cn/
// @icon         https://minesweeper.cn/favicon.ico
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 默认值
    const defaultOpacity = 0.3;
    const defaultBgImage = '';
    const defaultTitle = 'Tampermonkey - Minesweeper';

    // 获取存储的值,如果不存在则使用默认值
    let tableOpacity = GM_getValue('tableOpacity', defaultOpacity);
    let backgroundImage = GM_getValue('backgroundImage', defaultBgImage);
    let pageTitle = GM_getValue('pageTitle', defaultTitle);

    function hideElements() {
        const centerEl = document.querySelector('center');
        if (!centerEl) return;

        const allElements = centerEl.querySelectorAll('*');
        allElements.forEach(el => {
            // 只保留与 table 直接相关的并且保留嵌套在这些元素中的东西
            if (
                el.tagName.toLowerCase() !== 'table' &&
                el.tagName.toLowerCase() !== 'thead' &&
                el.tagName.toLowerCase() !== 'tbody' &&
                el.tagName.toLowerCase() !== 'tfoot' &&
                el.tagName.toLowerCase() !== 'tr' &&
                el.tagName.toLowerCase() !== 'td' &&
                el.tagName.toLowerCase() !== 'th' &&
                el.tagName.toLowerCase() !== 'caption' &&
                !el.closest('table')
            ) {
                el.hidden = true;
            }
            if (el.tagName.toLowerCase() === 'table') {
                el.style.opacity = tableOpacity;
            }
        });
    }

    function addBackgroundImage() {
        const bodyEl = document.querySelector('body');
        bodyEl.style.backgroundImage = `url("${backgroundImage}")`;
        bodyEl.style.backgroundSize = 'cover';
        bodyEl.style.backgroundRepeat = 'no-repeat';
        bodyEl.style.backgroundPosition = 'center';
    }

    function changePageTitle() {
        document.title = pageTitle;
    }

    function editPage() {
        hideElements();
        addBackgroundImage();
        changePageTitle();
    }

    editPage();

    const observer = new MutationObserver(() => {
        editPage();
    });

    const centerEl = document.querySelector('center');
    if (centerEl) {
        observer.observe(centerEl, {
            childList: true,
            subtree: true
        });
    }

    GM_registerMenuCommand('设置游戏区域透明度', () => {
        const newOpacity = prompt('请输入表格透明度 (0 到 1):', tableOpacity);
        if (newOpacity !== null && !isNaN(newOpacity)) {
            tableOpacity = parseFloat(newOpacity);
            GM_setValue('tableOpacity', tableOpacity);
            alert(`游戏区域透明度已设置为 ${tableOpacity}`);
            location.reload();
        }
    });

    GM_registerMenuCommand('设置背景图片地址', () => {
        const newBgImage = prompt('请输入背景图片的地址:', backgroundImage);
        if (newBgImage !== null) {
            backgroundImage = newBgImage;
            GM_setValue('backgroundImage', backgroundImage);
            alert(`背景图片地址已设置为 ${backgroundImage}`);
            location.reload();
        }
    });

    GM_registerMenuCommand('设置网页标题', () => {
        const newTitle = prompt('请输入网页标题:', pageTitle);
        if (newTitle !== null) {
            pageTitle = newTitle;
            GM_setValue('pageTitle', pageTitle);
            alert(`网页标题已被设置为 ${pageTitle}`);
            location.reload();
        }
    })
})();