Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have greasemonkey Check if text if found on page

I did do some research on google and the userscripts site but was unsuccessful in finding an answer.

So basically how can I check if specific text is found on a page? And the text is in no special tags or anything.

like image 468
RayB Avatar asked Feb 20 '11 21:02

RayB


2 Answers

A crude but fast way, for 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!");
}


For more focused searches use jQuery contains as in this previous question.

like image 115
Brock Adams Avatar answered Oct 31 '22 23:10

Brock Adams


for example this script will show if the text specific text is found on this page.

// ==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");

I don't know what you mean with special tags. Text is always inside some tags.

like image 4
wimh Avatar answered Oct 31 '22 22:10

wimh