admin管理员组文章数量:1195006
The content I am trying to modify has a series of <div>
entries, and within each of these are other <div>
entries. There are no id
tags to help here. What I want the script to do is inspect the content of each of these <div>
entries and look for some text. This will be used to determine whether the whole '' entry is deleted/hidden or not. Is this possible? How?
Below is an example. There are several of these in the page, and I want to delete/hide the ones where the text inside the <div class="foo bar">
tags say "Yes." So in this example, this whole thing would get deleted/hidden.
<div class="entry">
<div class="fooPhoto"><a href="Addfoo.jsp?tid=954102"><img class="person" src=".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 -->
The content I am trying to modify has a series of <div>
entries, and within each of these are other <div>
entries. There are no id
tags to help here. What I want the script to do is inspect the content of each of these <div>
entries and look for some text. This will be used to determine whether the whole '' entry is deleted/hidden or not. Is this possible? How?
Below is an example. There are several of these in the page, and I want to delete/hide the ones where the text inside the <div class="foo bar">
tags say "Yes." So in this example, this whole thing would get deleted/hidden.
<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 -->
Share
Improve this question
edited Jan 23, 2017 at 21:10
Brock Adams
93.4k23 gold badges240 silver badges304 bronze badges
asked Feb 6, 2012 at 23:44
node ninjanode ninja
33k60 gold badges173 silver badges257 bronze badges
1 Answer
Reset to default 22The general answer to your question is not too hard with the jQuery contains() selector. Something like this will work:
// ==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.entry div.foo:contains('Yes')");
badDivs.parent ().remove ();
Update for the site specified in the comments:
Note that there is no need to search for text because the site conveniently gives the key div a class of isX
or notX
.
// ==UserScript==
// @name _Sample
// @include http://example.com/SelectItem.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.notX");
badDivs.remove ();
Finally, for dynamic (AJAX driven) pages, use MutationObserver
or waitForKeyElements
. (WaitForKeyElements
also works fine on static pages.)
Here's the above script rewritten to be AJAX aware:
// ==UserScript==
// @name _Sample
// @include http://example.com/SelectItem.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", deleteNotX);
function deleteNotX (jNode) {
if (jNode.has("div.notX").length) {
jNode.remove ();
}
}
本文标签: javascriptHow to use greasemonkey to selectively remove content from a websiteStack Overflow
版权声明:本文标题:javascript - How to use greasemonkey to selectively remove content from a website - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738511318a2090831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论