使用Greasemonkey修改Javascript变量

时间:2012-04-16 14:26:48

标签: javascript variables greasemonkey

帮助,任何人都可以帮助我如何使用greasemonkey更改javascript变量吗?

我已经按照这个问题的答案: How can I change a JavaScript variable using Greasemonkey?

以及此链接: http://webdeveloper.com/forum/showthread.php?t=166658

但是我无法使用此脚本修改函数上的计数器变量值

// ==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;

以下是我想要更改的功能:

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;
    }
});
}

感谢您的帮助,我已经在google上搜索但仍然无法修改变量。

1 个答案:

答案 0 :(得分:1)

counter是函数中的局部变量。除非您可以修改函数以使用全局变量,否则您将无法更改其值。如果您无权访问代码,则可以尝试使用自己的实现替换整个startTimer函数。我无法从您的示例中看出,但如果它是全局的,您只能替换startTimer。