【问题标题】:How to use greasemonkey to selectively remove content from a website如何使用greasemonkey 有选择地从网站中删除内容
【发布时间】:2012-02-28 10:54:31
【问题描述】:

我要修改的内容有一系列 <div> 条目,每个条目中都有其他 <div> 条目。这里没有id 标签可以提供帮助。我希望脚本做的是检查每个 <div> 条目的内容并查找一些文本。这将用于确定整个 '' 条目是否被删除/隐藏。这可能吗?怎么样?

下面是一个例子。页面中有几个,我想删除/隐藏<div class="foo bar">标签内的文本说“是”的那些。所以在这个例子中,整个事情都会被删除/隐藏。

<div class="entry">

<div class="fooPhoto"><a href="Addfoo.jsp?tid=954102"><img class="person" src="http://static.barfoo.com/images/site/icons/dude.png" border="0" width="24" height="24" onerror="this.src='images/site/icons/dude.png'" title="Photo Unavailable" alt="Photo Unavailable" ></a></div>

<div class="fooAvg">4.7</div>     

<div class="foo bar">Yes</div>

<div class="fooShare">
<a class="shareEmail" href="referral.jsp?sid=882&tid=954102&pgid=3">Share using email</a>
</div>

</div><!-- closes entry -->

【问题讨论】:

    标签: javascript html greasemonkey webpage tampermonkey


    【解决方案1】:

    对于此类问题,请发布指向目标页面的链接。或者,如果这确实不可能,请将页面保存到 pastebin.com 并链接到该页面。

    无论如何,jQuery contains() selector 对您的问题的一般 答案并不难。这样的事情会起作用:

    // ==UserScript==
    // @name     _Remove annoying divs
    // @include  http://YOUR_SERVER/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @grant    GM_addStyle
    // ==/UserScript==
    //- The @grant directive is needed to restore the proper sandbox.
    
    /*--- Use the jQuery contains selector to find content to remove.
        Beware that not all whitespace is as it appears.
    */
    var badDivs = $("div div:contains('Annoying text, CASE SENSITIVE')");
    
    badDivs.remove ();
    
    //-- Or use badDivs.hide(); to just hide the content.
    


    更新新澄清的问题/代码:

    在你的具体例子中,你会使用这个:

    var badDivs = $("div.entry div.foo:contains('Yes')");
    
    badDivs.parent ().remove ();
    


    更新 cmets 中指定的站点:
    请注意,无需搜索文本,因为该站点方便地为键 div 提供了 isHotnotHot 类。

    // ==UserScript==
    // @name     _Unless they're hot, they're not (shown).
    // @include  http://www.ratemyprofessors.com/SelectTeacher.jsp*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @grant    GM_addStyle
    // ==/UserScript==
    //- The @grant directive is needed to restore the proper sandbox.
    
    var badDivs = $("#ratingTable div.entry").has ("div.notHot");
    
    badDivs.remove ();
    


    最后,对于动态(AJAX 驱动)页面,

    使用MutationObserverwaitForKeyElements。 (WaitForKeyElements 也适用于静态页面。)

    这是重写为支持 AJAX 的上述脚本:

    // ==UserScript==
    // @name     _Unless they're hot, they're not (shown).
    // @include  http://www.ratemyprofessors.com/SelectTeacher.jsp*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // ==/UserScript==
    //- The @grant directive is needed to restore the proper sandbox.
    
    waitForKeyElements ("#ratingTable div.entry", deleteNotHot);
    
    function deleteNotHot (jNode) {
        if (jNode.has("div.notHot").length) {
            jNode.remove ();
        }
    }
    

    【讨论】:

    • 即使它确实做了一些事情,看起来你的例子也行不通。删除包含文本的 div 是不够的。该 div 所在的 div 也必须删除。
    • 感谢您为您的问题添加一些细节。查看更新的答案。
    • 其实 foobar 里面有一个空格,所以它是“foo bar”。那么代码是什么?
    • 一行会变成:var badDivs = $("div.entry div.foo:contains('Yes')");(都在一行上)
    • 所以你的意思是如果 div 的类名中有空格,你只使用它的第一个单词?我试过了,它不起作用。实际上 entry div 也是两个词。
    猜你喜欢
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多