0

I have this HTML piece. <div class="text">helloworld</div>

So I would like to search the class "text" for a specific string, when that string is found in that class. Do a function, but only if the string was found, if the string wasn't found simply print console.log('not found.') and if it was simply print that it was.

My code is shown below.

var timerVar = setInterval (function() {DoMeEverySecond (); }, 2000); // << set to 2 seconds.

function DoMeEverySecond ()
{
var xpathResult = document.evaluate("(//text()[contains(., 'helloworld')]) [1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
var node=xpathResult.singleNodeValue;
if (node==null)
console.log('Not found')
else
console.log('found');
}

This code works, but it searches all classes. I would like to only search the class "text"

zer00ne
  • 41,936
  • 6
  • 41
  • 68

1 Answers1

0

Please check the below code, first of all you find all nodes (refer to this Q&A) with class contains 'text'. After that check each of nodes contains the words 'helloworld'

function getElementsByXPath(xpath, parent)
{
    let results = [];
    let query = document.evaluate(xpath, parent || document,
        null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (let i = 0, length = query.snapshotLength; i < length; ++i) {
        results.push(query.snapshotItem(i));
    }
    return results;
}

function DoMeEverySecond ()
{
 var nodes = getElementsByXPath("//*[contains(@class,'text')]", document);
 for(var i =0; i< nodes.length; i++){
  var xpathResult = document.evaluate("(text()[contains(., 'helloworld')]) [1]", nodes[i], null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  var node=xpathResult.singleNodeValue;
  if (node==null)
   console.log('Not found')
  else
   console.log('found');
 }
 
}

var timerVar = setInterval (function() {DoMeEverySecond (); }, 2000); // << set to 2 seconds.
Chiến Nghê
  • 736
  • 2
  • 9
  • 25
  • Let's say the Element with the class appears. It finds the text, It sends the log "Found". Now lets say. The element containing the text disappears. It's still printing in the log that it's found after the element disappears. – Sparkles the unicorn Jun 02 '19 at 14:28
  • @Sparklestheunicorn: Got your idea, I edited my answer :) – Chiến Nghê Jun 02 '19 at 14:56
  • In log works fine, it's ``not found``until I type helloworld as a chat message and send it making the element containing the class and string. Which would make it say ``found`` in the console, But when this happens it sends the ``found`` and the ``not found`` in the console. Sending both. Like it's doing both functions after it finds the element. – Sparkles the unicorn Jun 02 '19 at 16:01
  • I thought that it always re-scanned your document at each interval. If you would like to only trace newly added elements (new message). You need mechanism to mark elements as "scanned" (maybe by adding markup class to the DOM element) after each interval check. Scanned elements should be ignored in the next interval by adjusting xpath expressions. – Chiến Nghê Jun 02 '19 at 16:10