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 :(

0

1 Answer 1

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?

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

5 Comments

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 )
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 ?
Yes, you could parse all of the page's code, but that's a really poor idea in practice. See here for one of the many reasons.
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
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.

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.