我使用以下TreeWalker作为此帖子https://stackoverflow.com/a/37178130/7102491的模板,并将其修改为跳过元素“脚本”,以防止像Google搜索这样的某些网页无法使用。有谁知道如何更改代码以防止破坏某些页面?感谢。
var replaceArry = [
[/b/gi, 'better'],
[/Terms of service/gi, 'Términos y condiciones'],
[/Privacy policy/gi, 'Privacidad'],
// etc.
];
var numTerms = replaceArry.length;
var txtWalker = document.createTreeWalker (
document.body,
NodeFilter.SHOW_TEXT,
{ acceptNode: function (node) {
//-- Skip whitespace-only nodes
if (node.nodeValue.trim() && node.parentNode.nodeName != 'SCRIPT')
return NodeFilter.FILTER_ACCEPT;
return NodeFilter.FILTER_SKIP;
}
},
false
);
var txtNode = null;
while (txtNode = txtWalker.nextNode () ) {
var oldTxt = txtNode.nodeValue;
for (var J = 0; J < numTerms; J++) {
oldTxt = oldTxt.replace (replaceArry[J][0], replaceArry[J][1]);
}
txtNode.nodeValue = oldTxt;
}