如何使用greasemonkey有选择地删除网站上的内容 [英] How to use greasemonkey to selectively remove content from a website

查看:119
本文介绍了如何使用greasemonkey有选择地删除网站上的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



下面是一个例子。这些页面中有几个,我想要删除/隐藏< div class =foo bar> 标签内的文本说是。因此,在这个例子中,这件事会被删除/隐藏。

 < div class =entry> 

< div class =fooPhoto>< a href =Addfoo.jsp?tid = 954102>< img class =personsrc =http:// static .barfoo.com / images / site / icons / dude.pngborder =0width =24height =24onerror =this.src ='images / site / icons / dude.png'title =照片不可用alt =照片不可用>< / a>< / div>

< div class =fooAvg> 4.7< / div>

< div class =foo bar>是< / div>

< div class =fooShare>
< a class =shareEmailhref =referral.jsp?sid = 882& tid = 954102& pgid = 3>使用电子邮件分享< / a>
< / div>

< / div><! - 关闭条目 - >


解决方案

对于这样的问题,发布一个链接到目标页。或者,如果这是不可能的,请将该页面保存到 pastebin.com 并链接到该页面。



无论如何, general 对您的问题的回答并不太难, jQuery包含()选择器。这样的东西可以工作:

  // == UserScript == 
// @name _Remove恼人的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 ==
// - 需要@grant指令来恢复正确的沙箱。

/ * ---使用jQuery包含选择器来查找要删除的内容。
请注意,并非所有的空格都是如此。
* /
var badDivs = $(div div:contains('Annoying text,CASE SENSITIVE'));

badDivs.remove();

// - 或者使用badDivs.hide();隐藏内容。









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

在您的具体示例中,您可以使用以下代码:

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

badDivs.parent().remove();









在注释中指定的站点的更新:

请注意,不需要搜索文本,因为站点方便地为关键div提供了 isHot notHot

  // == UserScript == 
// @name _除非它们很热,它们没有(显示)。
// @包括http://www.ratemyprofessors.com/SelectTeacher.jsp*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/ jquery.min.js
// @grant GM_addStyle
// == / UserScript ==
// - 需要@grant指令来恢复正确的沙箱。

var badDivs = $(#ratingTable div.entry)。has(div.notHot);

badDivs.remove();









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



使用 MutationObserver waitForKeyElements 。 (WaitForKeyElements在静态页面上也能正常工作。)



以上脚本被重写为支持AJAX:

  // == UserScript == 
// @name _Unless他们很热,他们没有(显示)。
// @包括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 = =
// - 需要@grant指令来恢复正确的沙箱。

waitForKeyElements(#ratingTable div.entry,deleteNotHot);

函数deleteNotHot(jNode){
if(jNode.has(div.notHot)。length){
jNode.remove();
}
}


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 -->

解决方案

For questions like this, post a link to the target page. Or, if that is really not possible, save the page to pastebin.com and link to that.

Anyway, the 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 div:contains('Annoying text, CASE SENSITIVE')");

badDivs.remove ();

//-- Or use badDivs.hide(); to just hide the content.



Update for the newly clarified question/code:

In your specific example, you would use this:

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 isHot or 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 ();



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     _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 ();
    }
}

这篇关于如何使用greasemonkey有选择地删除网站上的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆