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