Help with hiding DIV tags, based on text content, using Greasemonkey

拥有回忆 提交于 2019-12-22 09:51:22

问题


I am looking for a way to write a Greasemonkey script which will take the following snippet of code and only show the code blocks consisting of <div class="A" where both "passtest" and "State1" are present.

<div class="A">
    <div class="B">
        <a href="/link1">
        <img class="imgClass" src="http://link.com/img.img" title="imgTitle"/>
        </a>
    </div>
    <div class="C">
        <span class="sc1">passtest</span>
        <br/>
        <em class="ec1">City1, State1</em>
    </div>
</div>
<div class="A">
    <div class="B">
        <a href="/link1">
        <img class="imgClass" src="http://link.com/img.img" title="imgTitle"/>
        </a>
    </div>
    <div class="C">
        <span class="sc1">failtest </span>
        <br/>
        <em class="ec1">City1, State1 </em>
    </div>
</div>
<div class="A">
    <div class="B">
        <a href="/link1">
        <img class="imgClass" src="http://link.com/img.img" title="imgTitle"/>
        </a>
    </div>
    <div class="C">
        <span class="sc1">passtest </span>
        <br/>
        <em class="ec1">City2, State2 </em>
    </div>
</div>

I found this from Dive Into Greasemonkey:

var allDivs, thisDiv;
allDivs = document.evaluate("//div[@class='sponsoredlink']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < allDivs.snapshotLength; i++) {
    thisDiv = allDivs.snapshotItem(i); 
    // do something with thisDiv 
}

I am looking at this code as the starting point for what I want to do. But, I am just a user, not a coder.

I understand the logic I need is:
For each div where class="a" which does contain the text "passtest" and also does not contain "state1" do not display that div.


回答1:


Here's a script that does that, using jQuery. The hard part is choosing the selectors.

// ==UserScript==
// @name     Show State1 passes only.
// @include  http://YOUR_SITE/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

$("div.A")  .hide ()
            .has ("span.sc1:contains('passtest')")
            .has ("em.ec1:contains('State1')")
            .show ();

Note that :contains() is case-sensitive.

See the code in action at jsFiddle.



来源:https://stackoverflow.com/questions/6921165/help-with-hiding-div-tags-based-on-text-content-using-greasemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!