1

Help, can anyone help me how to change javascript variable using greasemonkey?

I already following the answers on this question: How can I change a JavaScript variable using Greasemonkey?

and also this link: http://webdeveloper.com/forum/showthread.php?t=166658

but I unable to modify the counter variable value on the function with this script:

// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      https://welcome.telkomhotspot.info/telkomhs/freemax/
// @copyright  2012+, You
// ==/UserScript==

unsafeWindow.counter = 1;

Here are the function that I want to change:

function startTimer(){
var counter = 20;
var wait = 0;
var displayCounter = true;
var startCounting = false;
$("#timerTransition").html(getLoadingCounter())
.everyTime(1000,function(i){
    if(wait==0){
        if(displayCounter){
            $("#loadingCounter").fadeOut(500,function(){
                $("#timerTransition").html(getTimerContainer(counter));
                $("#timerContainer").fadeIn(500,function(){
                    startCounting = true;
                });
            });
        }
        displayCounter = false;
        if(startCounting){
            counter = counter - 1;
            $("#counter").html(counter);
            if(counter == 0) {

                if(foundCookies){
                    $("#timerTransition").stopTime().html(getAuthCookiesLogin());
                }
                else{
                    $("#timerTransition").stopTime().html(getAuthButton());
                    $("#authBtnContainer").fadeIn(0).click(function(){
                        $(this).fadeOut(0);
                        closeAds();
                        openAuthForm();
                    });
                }
            }
        }
    }
    else{
        wait = wait-1;
    }
});
}

Thanks for helping, I already searching on google but still I can't modify the variable.

Community
  • 1
  • 1
yudayyy
  • 260
  • 2
  • 9
  • 20
  • 1
    If you define `unsafeWindow.counter` you should also read `unsafeWindow.counter` so replace `counter` with `unsafeWindow.counter` everywhere. – noob Apr 16 '12 at 14:30
  • Did you mean, replace all `counter` with `unsafeWindow.counter`? But I don't have an access to the file. I want to modify the counter variable **value** from `counter = 20;` with `counter = 1;` on some website using Greasemonkey, so I can skip the **timer** more quickly. Can you tell me how? – yudayyy Apr 16 '12 at 15:13

1 Answers1

1

counter is a local variable within a function. You will not be able to change its value unless you can modify the function to use a global variable. If you do not have access to the code, you could try replacing the entire startTimer function with your own implementation. I can not tell from your example, but you can only replace startTimer if it is a global.

mikerobi
  • 20,527
  • 5
  • 46
  • 42
  • Thanks @mikerobi, I'm trying to replace the function with new simple script on this website: [link](http://www.squakmt.com/replacing_javascript/index.htm). but the `startTimer` function on `idconnect.js` file still being called by the page. Well, i must say I'm still new with Greasemonkey. – yudayyy Apr 16 '12 at 16:04
  • @yudayyy, I forgot to mention, you can only replace startTimer if it is a global as well. – mikerobi Apr 16 '12 at 17:56
  • so, it mean I cannot override the function on `idconnect.js` right? – yudayyy Apr 16 '12 at 19:32