:contains()不返回预期的节点 请考虑下面的代码: // @include /(^https?:\/\/www\.|^https?:\/\/)google\.com(\.|\/).*/ // @include /(^https?:\/\/www\.|^https?:\/\/)stackoverflow\.com\/.*/ // @include /(^https?:\/\/www\.|^https?:\/\/)userscripts-mirror\.org\/.*/ // @include http://wiki.greasespot.net/* // @version 1.0 // @require https://code.jquery.com/jquery-2.1.1.min.js // @grant GM_addStyle $ ( 'body:contains("serscripts.org")').each ( function () { var results = $(this); var text = results.text(); console.log(text); text = text.replace( /serscripts\.org/ig, "serscripts-mirror.org" ); text = text.replace( /(:8080|%3A8080)/ig, ""); //results.text ( text ); } );

:contains()不返回预期的节点 请考虑下面的代码: // @include /(^https?:\/\/www\.|^https?:\/\/)google\.com(\.|\/).*/ // @include /(^https?:\/\/www\.|^https?:\/\/)stackoverflow\.com\/.*/ // @include /(^https?:\/\/www\.|^https?:\/\/)userscripts-mirror\.org\/.*/ // @include http://wiki.greasespot.net/* // @version 1.0 // @require https://code.jquery.com/jquery-2.1.1.min.js // @grant GM_addStyle $ ( 'body:contains("serscripts.org")').each ( function () { var results = $(this); var text = results.text(); console.log(text); text = text.replace( /serscripts\.org/ig, "serscripts-mirror.org" ); text = text.replace( /(:8080|%3A8080)/ig, ""); //results.text ( text ); } );,jquery,replace,greasemonkey,Jquery,Replace,Greasemonkey,我希望返回的应该只是包含该字符串的元素,但控制台上的结果是主体的所有文本 在第行考虑: 我想改变这一点 或: 我也想换衣服 标记之间的所有单词都应该检查字符串,并且只返回具有该字符串的单词 此选项的正确选择器是什么 注意: 之间的链接属性和文本已使用类似的进行更改。每个(函数):contains返回的正是预期的内容。发件人: 匹配文本可以直接出现在所选元素、该元素的任何子元素、或其组合中 通常,要替换、包装或突出显示网页上的文本术语,您需要在单个text\u节点级别工作。其他任何东西都

我希望返回的应该只是包含该字符串的元素,但控制台上的结果是主体的所有文本


在第行考虑:

我想改变这一点


或:

我也想换衣服


标记之间的所有单词都应该检查字符串,并且只返回具有该字符串的单词

此选项的正确选择器是什么


注意

之间的链接属性和文本已使用类似的
进行更改。每个(函数)
:contains
返回的正是预期的内容。发件人:

匹配文本可以直接出现在所选元素、该元素的任何子元素、或其组合中


通常,要替换、包装或突出显示网页上的文本术语,您需要在单个
text\u节点
级别工作。其他任何东西都有被破坏的风险:URL、id、事件处理程序等

为此,您可以使用一个:

var txtWalker   = document.createTreeWalker (
    document.body,
    NodeFilter.SHOW_TEXT,
    {   acceptNode: function (node) {
            //-- Skip whitespace-only nodes
            if (node.nodeValue.trim() )
                return NodeFilter.FILTER_ACCEPT;

            return NodeFilter.FILTER_SKIP;
        }
    },
    false
);
var txtNode     = null;

while (txtNode  = txtWalker.nextNode () ) {
    var oldTxt  = txtNode.nodeValue;
    var newTxt  = oldTxt.replace (/serscripts\.org/ig, "serscripts-mirror.org");
    newTxt      = newTxt.replace (/(:8080|%3A8080)/ig, "");

    txtNode.nodeValue = newTxt;
}
这将安全地处理页面上的所有内容,但HTML属性除外,如
href
src


如果您希望更改它们,请使用单独的、有针对性的
。每个()
代码——就像您所说的那样——但不要在
之间更改文本,因为树行者已经完成了更改。

您选择了
元素,其中包含
serscripts.org
。你完全明白了。你可能想在分号前面加一个空格,去掉
body
selector@DJDavid98我没弄明白,为什么?谢谢你,布罗克,效果很好。我删除了“标签之间”处理程序
// @require http://userscripts.org/scripts/source...
var txtWalker   = document.createTreeWalker (
    document.body,
    NodeFilter.SHOW_TEXT,
    {   acceptNode: function (node) {
            //-- Skip whitespace-only nodes
            if (node.nodeValue.trim() )
                return NodeFilter.FILTER_ACCEPT;

            return NodeFilter.FILTER_SKIP;
        }
    },
    false
);
var txtNode     = null;

while (txtNode  = txtWalker.nextNode () ) {
    var oldTxt  = txtNode.nodeValue;
    var newTxt  = oldTxt.replace (/serscripts\.org/ig, "serscripts-mirror.org");
    newTxt      = newTxt.replace (/(:8080|%3A8080)/ig, "");

    txtNode.nodeValue = newTxt;
}