-2

I'm writing a Greasemonkey script, which should do a simple thing: if there's a certain string between the <h2> element (always only one on the page) and the first occurrence of an <h3> element (can be several of them or none at all), then ... (I've already done this part).

I'd be grateful for some help with the conditional.

Mikhail
  • 1,326
  • 6
  • 22
  • 29

4 Answers4

2
var masterString = document.body.innerHTML;
var start = masterString.indexOf('<h2>'); 
var end = masterString.indexOf('<h3>'); // Will find the position of first occurance of h3 - if any

if (end <0)
// there is no h3
else {
  var searchMe = masterString.substr(start,end);  // now this is the portion of your HTML body that you want to look for a string match
  var numberOfOccurances = searchMe.match(/yourString/g);
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
balafi
  • 2,143
  • 3
  • 17
  • 20
  • jQuery is not part of this question, and you didn't show how to add it to a GM script (which is not a given for newish scripters). Hence, change that 1 line. Then, this answer may be the best one can do with the vagueness of the problem description, so +1. ... BUT, what happens if the code is uppercase (`

    `), etc. ? ;-)

    – Brock Adams Oct 08 '12 at 16:18
  • What happens if the node has attributes? EG `

    ` Or if there is an `

    ` ***before*** the `

    `?

    – Brock Adams Oct 08 '12 at 16:24
  • You're absolutely right. There a lot of cases that this will not do the work or will create problems since it parses the html source code. But it could be a quick and dirty solution under certain circumstances. – balafi Oct 08 '12 at 17:05
2

now I just happen to be familiar with the codes Mikhail are working on.

The basic structure are

<div>
<div>
<h2>Something</h2>
<td>We want to search for the string here</td>
<td>We want to search for the string here</td>
</div>

<div>
<h3>Something else</h3>
<td>May contain the same string, but we are only interested if it contains in previous div .</td>
<td>May contain the same string, but we are only interested if it contains in previous div .</td>
</div>
</div>

The string is not a child of h2, so I don't think getElementsByTagName would work. Unfortunately there are literally hundreds of div layer with same class id. In this particular case heading is the only unique details in the code. So in my opinion the best way is to find h2 first, go to its parent and store text as string. Then search for the string in the text. Soemthing like this...

<script>
var searcharea = jQuery('h2').parent('div').text();
var searchstring = "superstring";
if( searcharea.indexOf( searchstring ) != -1 )
    alert("exchange alert to your own things");
</script>

As for h3, it may or may not exist, but since its not a sibling, it doesn't really matter. :) Thank you all for taking your time to answer our question.

Patrick
  • 56
  • 1
  • 5
0

Have you tried getElementsByTagName?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Yeah, I believe this is what I should use. Unfortunately, I've got very basic knowledge of JavaScript, so if you could elaborate... :-) – Mikhail Oct 08 '12 at 16:00
0
//get all the H2 elements
var h2 = document.getElementsByTagName("h2");

//Just in case there are more than one...
for(var i = 0; i < h2.length; i++){
 //check for the certain string
 if(h2[i].textContent == "SOME TEXT"){
  //get the first h3 element and do something...
  var h3 = document.getElementsBtTagName("h3")[0]; //first occurrence of H3
  //DO SOMETHING
}

https://developer.mozilla.org/en-US/docs/DOM/Node.textContent

https://developer.mozilla.org/en-US/docs/DOM/element.getElementsByTagName

Chase
  • 29,019
  • 1
  • 49
  • 48