如何使用greasemonkey检查是否在页面上找到了文本

时间:2011-02-20 21:02:40

标签: text greasemonkey

我确实在google和userscripts网站上做了一些研究,但未能找到答案。

所以基本上如何检查页面上是否找到特定文本? 并且文本没有特殊的标签或任何东西。

2 个答案:

答案 0 :(得分:5)

对于FF GM来说,这是一种粗暴但快速的方式:

if (/Text you are looking for/i.test (document.body.innerHTML) )
{
    alert ("Found it!");
}

//--- Looking for one of two different texts...
if (/(Text ONE that you are looking for)|(Text TWO that you are looking for)/i.test (document.body.innerHTML) )
{
    alert ("Found one!");
}


对于更有针对性的搜索,请使用jQuery contains中的this previous question

答案 1 :(得分:3)

例如,此脚本将显示是否在此页面上找到文本specific text

// ==UserScript==
// @name           so5059986
// @namespace      test
// @description    test
// @include        http://stackoverflow.com/questions/5059986/how-to-have-greasemonkey-check-if-text-if-found-on-page
// ==/UserScript==

var xpathResult = document.evaluate("(//text()[contains(., 'specific text')])[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
var node=xpathResult.singleNodeValue;
if (node==null)
    alert("text not found");
else
    alert("text found on page");

我不知道你对特殊标签的意思。文本总是在某些标签内。

相关问题