Greasy Fork is available in English.
Replace Chinese text with English text on any website
当前为
// ==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 });
})();