// 合并两个脚本的功能
(function() {
    'use strict';

    // 激活状态变量
    let isActivated = false;

    // 替换状态变量
    let isReplacingFlash = false;

    // 激活函数，需要输入密码
    function activateScript(password) {
        const correctPassword = "705878475"; // 设置您的密码
        if (password === correctPassword) {
            isActivated = true;
            GM_setValue("isActivated", true);
            alert("脚本已激活！");
        } else {
            alert("密码错误！");
        }
    }

    // 切换Flash替换功能
    function toggleReplaceFlash() {
        isReplacingFlash = !isReplacingFlash;
        GM_setValue("isReplacingFlash", isReplacingFlash);
        alert(isReplacingFlash ? "Flash替换功能已开启！" : "Flash替换功能已关闭！");
    }

    // 检查激活状态
    function checkActivation() {
        if (!isActivated && !GM_getValue("isActivated", false)) {
            const password = prompt("请输入密码以激活脚本：", "");
            if (password) {
                activateScript(password);
            }
        }
    }

    // 注册菜单命令
    function registerMenuCommands() {
        GM_unregisterMenuCommand("点我注入");
        GM_registerMenuCommand("点我注入", () => {
            checkActivation();
            if (isActivated || GM_getValue("isActivated", false)) {
                // 刷新当前页面
                location.href = location.href;
                alert("页面已刷新，脚本功能已尝试执行！");
            } else {
                alert("脚本未激活，请先激活！");
            }
        });

        GM_registerMenuCommand("切换Flash替换功能", toggleReplaceFlash);
    }

    // 替换Flash内容的函数
    const replaceFlashWithRuffle = (element) => {
        const src = element.getAttribute('src');
        if (src && src.endsWith('.swf')) {
            const container = element.parentNode;
            const width = element.getAttribute('width') || '100%';
            const height = element.getAttribute('height') || '100%';

            // Remove the old Flash element
            element.parentNode.removeChild(element);

            // Add the Ruffle player
            const ruffleDiv = document.createElement('div');
            ruffleDiv.setAttribute('data-ruffle', 'https://unpkg.com/@ruffle-rs/ruffle');
            ruffleDiv.style.width = width;
            ruffleDiv.style.height = height;
            container.appendChild(ruffleDiv);
        }
    };

    // 替换Flash内容
    const replaceAllFlashContent = () => {
        const elementsToCheck = ['object', 'embed', 'iframe'];
        elementsToCheck.forEach(tagName => {
            const elements = document.getElementsByTagName(tagName);
            for (let i = 0; i < elements.length; i++) {
                replaceFlashWithRuffle(elements[i]);
            }
        });
    };

    // 监听DOM变化
    const observer = new MutationObserver(() => {
        if (isReplacingFlash) {
            replaceAllFlashContent();
        }
    });

    // 配置观察者
    const config = { childList: true, subtree: true };

    // 开始监听body的变化
    observer.observe(document.body, config);

    // 初始化
    registerMenuCommands();

    // 加载Ruffle脚本
    const ruffleScriptSrc = 'https://unpkg.com/@ruffle-rs/ruffle';
    const script = document.createElement('script');
    script.src = ruffleScriptSrc;
    script.onload = () => {
        console.log('Ruffle script loaded.');
        if (isReplacingFlash || GM_getValue("isReplacingFlash", false)) {
            replaceAllFlashContent();
        }
    };
    script.onerror = () => {
        console.error('Failed to load Ruffle script.');
    };
    document.head.appendChild(script);
})();