1

I want to learn how to make a script in Greasemonkey to modify variables in a page or bypass a delaying function.

Here is the script found in the middle of the page:

<script language="Javascript">
    x300=100;

    function countdown() {
        x300--;
        if (x300 == 0) {
            document.getElementById('countdown').innerHTML =
                '<strong>Proceed to URL - <a href="http://XXXX.com/11123324">click here</a>!</strong>';
        }
        if (x300 > 0) {
            document.getElementById('countdown').innerHTML =
                'You will be redirected in ' + x300 + ' seconds.';
            setTimeout('countdown()',100000);
        }
    }

    countdown();
</script>


Of course I want to do a script that will redirect me to http://XXXX.com/11123324, I am still noob so the best script I made was:

// ==UserScript==
// @name        aaa
// @include     bbbb
// @grant       none
// ==/UserScript==

x300=1;
countdown()

but it didn't work :(

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Leo92
  • 724
  • 4
  • 16
  • 29

1 Answers1

1

Update:

Based on the original question (now deleted), and the apparent target page, the variable x300 and the function countdown() are global. But the variable name x300 actually changes from page to page. This means that it is not a good choice for scripting.

The function name countdown() appears to be constant though. So, you can extract the desired link from the function source using regex:

// ==UserScript==
// @name        aaa
// @include     bbbb
// @grant       none
// ==/UserScript==

var addrFound       = false;
var cntDwnFuncSrc   = window.countdown;  // Use unsafeWindow if grant is not none.
if (cntDwnFuncSrc) {
    cntDwnFuncSrc           = countdown.toString ();
    var payloadAddrMatch    = cntDwnFuncSrc.match (/href="([^"]+?)"/);
    if (payloadAddrMatch  &&  payloadAddrMatch.length > 1) {
        location.assign (payloadAddrMatch[1]);
        addrFound           = true;
    }
}

if ( ! addrFound) {
    alert ("Payload address not found.  Site probably changed");
}



Given your sample page, your userscript should have worked. That it didn't, means you are omitting or changing a key detail in your question!

  1. What errors do you get in the error console (CtrlShiftJ)?

  2. What happens when you run the following code in the Firefox or Firebug console?

    console.log (x300);
    console.log (countdown);
    
  3. What is the URL of the target page so that we can see what is really going on?

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • its worked like a charm :) , thanks a lot . I will try to understand your code and to do many tests later because I must go now . again thank you a lot ) – Leo92 Mar 16 '13 at 10:03
  • I did a lot of tests and it worked all the time :) , I love the approach of getting the link using the regex . but I have a question , in this script you parse the function countdown (at least that what I think) so my question is , is it possbile to parse the all the page code not just the countdown function ? – Leo92 Mar 16 '13 at 13:41
  • Yes, you could parse all of the page's code, but that's a really poor idea in practice. See [**here**](http://stackoverflow.com/a/1732454/331508) for *one* of the many reasons. – Brock Adams Mar 16 '13 at 21:26
  • yeah I know its a painful process , I do a lot of html parsing using (grep and sed) and I know the risks . however sometimes its become the quickest available solution so can you tell me how to do it (what is the instruction ?) thanks for your time – Leo92 Mar 16 '13 at 21:36
  • That is another question. Ask a new one, **with specific details**, if necessary. However, many variations of that question have already been asked, so search SO before posting. – Brock Adams Mar 16 '13 at 22:09