对于此类问题,请发布指向目标页面的链接。或者,如果这确实不可能,请将页面保存到 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 提供了 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 ();
最后,对于动态(AJAX 驱动)页面,
使用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 ();
}
}