0

I am trying to write a code for Greasemonkey. I can handle the tagnames, ids etc. But I got stuck in the 'script' tag. I can get the innerHTML of this script tag. But I want to get the input parameters only of the 'on' function, specifically 4th input which is a number. And store them in an array.

<ol id="online1">
        <script language="javascript" >
            on('0','2','abc','401757','');
    on('1','1','asd','1115337','');
    on('2','2','asdsad','1169333','');
    gi('asdasd','10453','');
    gi('asdasd','10453','');
.
.
.
.
</script>

Should result in

array = [401757,1115337,1169333];

I can get the innerHTML of the script like this:

window.frames[2].document.getElementById('online1').getElementsByTagName('script')[0].innerHTML
ardavar
  • 510
  • 9
  • 20
  • 1
    If you got it as string, just parse it with regex. – MightyPork Sep 29 '14 at 18:48
  • Ok this is the first step. So you are saying that technically I can parse the contents between the 'on(' and ');' then parse the content between the 3rd and 4th comma. Can you give a bit more clue – ardavar Sep 29 '14 at 18:52

1 Answers1

1

As per your example, you can just use .split() on the innerHTML using , as a delimiter. If you want, you can add in an extra safeguard and use a function to detect whether you're getting a number.

JavaScript:

var vals = document.getElementsByTagName('script')[2].innerHTML.split(',');
for(var i = 3; i < vals.length; i+=4)
{
    console.log(vals[i]);
}

Output:

'401757'
'1115337'
'1169333'
'10453'

jsFiddle

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • This makes sense too. Is this approach correct then? 1-split using ; to get rows 2-split using , to get parameters 3-check if the first parameter is 'on' – ardavar Sep 29 '14 at 18:56
  • @ardavar Generally, that's the idea. Just keep in mind that if a comma or semicolon is missing this approach will fail. – Alex W Sep 29 '14 at 19:01
  • This is close enough, in my example it will fail because parameters hence the commas are not standard in all functions I will add some checkpoints to filter the functions. – ardavar Sep 29 '14 at 19:10