nod*_*nja 7 html javascript webpage greasemonkey tampermonkey
我试图修改的内容有一系列<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 -->
Run Code Online (Sandbox Code Playgroud)
Bro*_*ams 12
对于这样的问题,请发布指向目标页面的链接.或者,如果真的不可能,请将页面保存到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.
Run Code Online (Sandbox Code Playgroud)
更新新澄清的问题/代码:
在您的具体示例中,您将使用此:
var badDivs = $("div.entry div.foo:contains('Yes')");
badDivs.parent ().remove ();
Run Code Online (Sandbox Code Playgroud)
注释中指定的站点的更新:
请注意,不需要搜索文本,因为站点方便地为密钥div提供一个isHot或类的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 ();
Run Code Online (Sandbox Code Playgroud)
用MutationObserver或waitForKeyElements.(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 ();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23626 次 |
| 最近记录: |