Greasy Fork

Greasy Fork is available in English.

配置网页具体中文转换为英文

Replace Chinese text with English text on any website

当前为 2024-06-13 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         配置网页具体中文转换为英文
// @namespace    https://github.com/Whiskey-Liu
// @version      0.2
// @description  Replace Chinese text with English text on any website
// @author       Your Name
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 替换文本映射
    const translationMap = {
        '你好': 'Hello',
        '世界': 'World',
        '料号': 'model',
        // 添加更多的替换项
    };

    // 替换函数
    function replaceText(node) {
        let text = node.nodeValue;
        for (let [chinese, english] of Object.entries(translationMap)) {
            let regex = new RegExp(chinese, 'g');
            text = text.replace(regex, english);
        }
        node.nodeValue = text;
    }

    // 遍历所有文本节点
    function walk(node) {
        let child, next;

        switch (node.nodeType) {
            case 1:  // Element
            case 9:  // Document
            case 11: // Document fragment
                child = node.firstChild;
                while (child) {
                    next = child.nextSibling;
                    walk(child);
                    child = next;
                }
                break;

            case 3: // Text node
                replaceText(node);
                break;
        }
    }

    // 初始替换
    walk(document.body);

    // 观察 DOM 变化以处理动态内容
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            for (let node of mutation.addedNodes) {
                walk(node);
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();