Greasy Fork

来自缓存

Greasy Fork is available in English.

MT 繁体自动转简体

自动将 M-Team 网站的繁体中文转换为简体中文

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MT  繁体自动转简体
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动将 M-Team 网站的繁体中文转换为简体中文
// @author       softOS
// @match        https://kp.m-team.cc/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加简繁转换库 (OpenCC)
    const script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/t2cn.js';
    document.head.appendChild(script);

    script.onload = function() {
        // 确保库加载完成后执行转换
        const converter = OpenCC.Converter({ from: 'hk', to: 'cn' });

        // 转换函数
        function convertToCN() {
            // 转换页面中的文本节点
            const textNodes = [];
            const walk = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
            let node;
            while (node = walk.nextNode()) {
                // 排除脚本和样式中的文本
                if (node.parentElement &&
                    node.parentElement.tagName !== 'SCRIPT' &&
                    node.parentElement.tagName !== 'STYLE') {
                    textNodes.push(node);
                }
            }

            // 对所有文本节点进行繁体到简体的转换
            textNodes.forEach(node => {
                if (node.nodeValue.trim()) {
                    node.nodeValue = converter(node.nodeValue);
                }
            });

            // 转换页面标题
            if (document.title) {
                document.title = converter(document.title);
            }
        }

        // 初次执行转换
        convertToCN();

        // 监听动态内容变化,对新内容进行转换
        const observer = new MutationObserver(mutations => {
            let needConvert = false;

            for (const mutation of mutations) {
                if (mutation.addedNodes.length) {
                    needConvert = true;
                    break;
                }
            }

            if (needConvert) {
                // 使用延迟以确保DOM完全更新
                setTimeout(convertToCN, 100);
            }
        });

        // 开始监听页面变化
        observer.observe(document.body, { childList: true, subtree: true });
    };
})();