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
2
  • 1
    If you got it as string, just parse it with regex. Commented Sep 29, 2014 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 Commented Sep 29, 2014 at 18:52

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 Generally, that's the idea. Just keep in mind that if a comma or semicolon is missing this approach will fail.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.