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