我希望将名为“mylist”的 .json 列表中的字符串与仅在任何网页上出现的可见字符串进行匹配。匹配的字符串将被替换为下面脚本中的术语“ REPLACED! ”。
最重要的是脚本应该在现代浏览器上运行并且不会降低页面加载性能。所以我很感激任何优化建议!
这是我到目前为止所得到的:
@resource mylist https://example.com/mylist.json
(function(){
var mylist = JSON.parse(GM_getResourceText("mylist"));
var regexp = new RegExp('\\b(' + mylist.join('|') + ')\\b(?!\\)\\))', "gi");
function walk(node) {
var child, next;
switch ( node.nodeType )
{
case 1:
case 9:
case 11:
child = node.firstChild;
while ( child )
{
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3:
handleText(node);
break;
}
}
function handleText(textNode) {
textNode.nodeValue = textNode.nodeValue.replace(regexp, 'REPLACED!');
}
walk(document.body);
})();
Run Code Online (Sandbox Code Playgroud)
我在这里制作了一个示例: https: //jsfiddle.net/xgnocwbq/ …