2012-04-16 102 views
1

幫助,任何人都可以幫助我如何使用greasemonkey更改JavaScript變量?修改JavaScript變量使用Greasemonkey

我已經關注這個問題的答案: 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; 
    } 
}); 
} 

感謝您的幫助,我已經在谷歌搜索,但仍然無法修改變量。

+1

如果您定義'unsafeWindow.counter',您還應該閱讀'unsafeWindow.counter',以便在每個地方都用'unsafeWindow.counter'替換'counter'。 – noob 2012-04-16 14:30:03

+0

你是不是要用'unsafeWindow.counter'替換所有'counter'?但是我沒有訪問該文件的權限。我想使用Greasemonkey在'counter = 20;''counter = 1;'在某個網站上修改計數器變量**值**,所以我可以更快地跳過**計時器**。你能告訴我如何? – yudayyy 2012-04-16 15:13:39

回答

1

counter是函數內的局部變量。除非您可以修改函數以使用全局變量,否則您將無法更改其值。如果您無法訪問代碼,則可以嘗試用您自己的實現替換整個startTimer函數。我不能從你的例子中看出來,但是如果它是全局的,你只能替換startTimer。

+0

謝謝@mikerobi,我試圖用本新網站上的新簡單腳本替換函數:[鏈接](http://www.squakmt.com/replacing_javascript/index.htm)。但是'idconnect.js'文件上的'startTimer'函數仍然被頁面調用。 嗯,我必須說我還是Greasemonkey的新手。 – yudayyy 2012-04-16 16:04:49

+0

@yudayyy,我忘了提及,如果它是全局的,那麼您只能替換startTimer。 – mikerobi 2012-04-16 17:56:50

+0

所以,這意味着我不能重寫'idconnect.js'右邊的功能? – yudayyy 2012-04-16 19:32:26