9

我要修改的内容有一系列<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 -->
4

1 回答 1

16

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

无论如何,使用jQuery contains() 选择器对您的问题的一般答案并不难。像这样的东西会起作用:

// ==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 ();


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

// ==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 ();
    }
}
于 2012-02-07T00:10:23.900 回答