javascript-使用Greasemonkey帮助基于文本内容隐藏DIV标签
作者:互联网
我正在寻找一种编写Greasemonkey脚本的方法,该脚本将采用以下代码段,并且仅显示同时包含“ passtest”和“ State1”的< div class =“ A”组成的代码块.
<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>
我从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
}
我正在看这段代码,作为我想做的起点.但是,我只是一个用户,而不是编码人员.
我了解我需要的逻辑是:
对于确实包含文本“ passtest”且也不包含“ state1”的class =“ a”的每个div,不会显示该div.
解决方法:
这是一个使用jQuery来执行此操作的脚本.最困难的部分是选择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 ();
请注意07002.
See the code in action at jsFiddle.
标签:greasemonkey,javascript 来源: https://codeday.me/bug/20191102/1990745.html