代码如下,是我抄来的,作用是替换文本汉化网页
就是开头这个(RC)这行不生效,我试着在括号前面加一个反斜杠没成功,请问应该如何让它被替换成中文呢?
多谢各位大佬啦!!!QvQ
(function() {
'use strict';
const i18n = new Map([
['When this unit is placed on (RC) from hand', '这个单位从手牌登场到R时'],
['and put it into your drop if it isn\'t', '不是的话,放置到弃牌区'],
['Diabolos, "Violence" Bruce', '魔惧会 “暴虐” 布鲁斯'],
])
replaceText(document.body)
const bodyObserver = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(addedNode => replaceText(addedNode))
})
})
bodyObserver.observe(document.body, { childList: true, subtree: true })
function replaceText(node) {
nodeForEach(node).forEach(htmlnode => {
i18n.forEach((value, index) => {
// includes可直接使用 === 以提高匹配精度
const textReg = new RegExp(index, 'g')
if (htmlnode instanceof Text && htmlnode.nodeValue.includes(index)) {
htmlnode.nodeValue = htmlnode.nodeValue.replace(textReg, value)}
else if (htmlnode instanceof HTMLInputElement && htmlnode.value.includes(index)) {
htmlnode.value = htmlnode.value.replace(textReg, value)}
})
})
}
function nodeForEach(node) {
const list = []
if (node.childNodes.length === 0) list.push(node)
else {
node.childNodes.forEach(child => {
if (child.childNodes.length === 0) list.push(child)
else list.push(...nodeForEach(child))
})
}
return list
}
})();