ba-hub汉化

将特定网页内容从英文翻译成中文,支持动态加载。

// ==UserScript==
// @name         ba-hub汉化
// @namespace    http://tampermonkey.net/
// @version      0.8
// @description  将特定网页内容从英文翻译成中文,支持动态加载。
// @author       zola
// @match        https://www.ba-hub.net/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 定义翻译映射
    const translations = {
        // 第一次提供的翻译
        "Abilities": "能力",
        "Modifications": "修改",
        "Weapon package": "武器包",
        "Vehicle": "载具",
        "Mobility": "机动性",
        "Optics": "光学",
        "Weapons": "武器",
        // 第二次提供的翻译
        "Front": "前方",
        "Sides": "侧面",
        "Rear": "后方",
        "Top": "顶部",
        "Cost": "成本",
        "Health": "生命值",
        "Weight": "重量",
        "Stealth": "隐蔽性",
        "Table View": "表格视图",
        // 第三次提供的翻译
        "Type": "类型",
        "AIM": "瞄准",
        "REL": "可靠性",
        "ROF": "射速",
        "NOI": "噪音",
        "Magazine": "弹匣",
        "DMG": "伤害",
        "STR": "力量",
        "PEN": "穿透",
        "RNG": "射程",
        "Target Types": "目标类型",
        "Ignore Cover": "无视掩体",
        "Resupply Cost": "补给成本",
        "Interceptable": "可拦截",
        "DIS": "距离",
        // 第四次提供的翻译
        "Resupply Time": "补给时间",
        "Trajectory": "弹道",
        "Traits": "特性",
        "USA": "美国",
        "Projectile Information": "弹药信息",
        "Rotation Speed": "旋转速度",
        "Acceleration": "加速度",
        "Max Speed": "最大速度",
        "Muzzle Velocity": "初速",
        "Kinetic": "动能",
        "Armor package": "装甲包",
        // 第五次提供的翻译
        "Home": "首页",
        "Arsenal": "武器库",
        "Deck Editor": "卡组编辑器",
        "Intel": "情报",
        "Maps": "地图",
        "Calculators": "计算器",
        "Community": "社区",
        "Discord": "Discord",
        // 第六次提供的翻译
        "USMC": "美国海军陆战队",
        "Armored Brigade": "装甲旅",
        "Airborne infantry": "空降步兵",
        "VDV Brigade": "VDV旅",
        "Guard Tank Brigade": "卫队坦克旅",
        "Coastal Troops": "沿海部队",
        // 第七次提供的翻译
        "Country": "国家",
        "Spec": "规格",
        "Category": "类别",
        "Sort By": "排序方式",
        "Russia": "俄罗斯",
        "Name": "名称",
        "Recon": "侦察",
        "Infantry": "步兵",
        "Support": "支援",
        "Helicopter": "直升机",
        "Airplane": "飞机",
        "Transport": "运输"
    };

    // 翻译单个元素的函数
    function translateElement(el) {
        const text = el.textContent.trim();
        if (translations.hasOwnProperty(text)) {
            el.textContent = translations[text];
        }
    }

    // 翻译所有匹配元素的函数
    function translateAll() {
        // 针对 .MuiTypography-root, .MuiMenuItem-root 和 .MuiFormLabel-root 内的文本进行翻译
        const elements = document.querySelectorAll('.MuiTypography-root, .MuiMenuItem-root, .MuiFormLabel-root');
        elements.forEach(translateElement);
    }

    // 首次加载时执行翻译
    translateAll();

    // 设置 MutationObserver 监听 DOM 变化
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        if (node.classList.contains('MuiTypography-root') ||
                            node.classList.contains('MuiMenuItem-root') ||
                            node.classList.contains('MuiFormLabel-root')) {
                            translateElement(node);
                        }
                        const descendants = node.querySelectorAll('.MuiTypography-root, .MuiMenuItem-root, .MuiFormLabel-root');
                        descendants.forEach(translateElement);
                    }
                });
            }
        });
    });

    // 监听整个文档的变化
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();