2015-02-11 16 views
0

我的目標是去http://quizlet.com/12039115/scatter並在2秒內得分。我的計劃是通過使用setInterval/clearInterval禁用定時器來完成此操作。 我從一些網站上取下了一些代碼,並嘗試將其適用於我的目的;它失敗了。現在我需要知道哪裏出了問題。 原始代碼可在blog.jazzychad.net/2011/03/20/inspect-javascript-timers-greasemonkey.html找到。當我裝這Tampermonkey並運行它在頁面上,只有setInterval的打印出來(多次):這個篡改密碼有什麼錯誤?

INSPECT_TIMERS: setInterval - 100ms 
quizlib.2X5g7.js:340 
INSPECT_TIMERS: function(){return c.apply(b,a||arguments)} 

因此,我可以看到它發現的計時器ID。現在我需要clearInterval()。這裏是東西出錯的地方。這給了上面的輸出

代碼:

var go = function(window){ 

    var oldSetInterval = window.setInterval; 
    var newSetInterval = function(f,t) { 
     __log("INSPECT_TIMERS: setInterval - " + t + "ms"); 
     __log("INSPECT_TIMERS: " + f); 
     var id = oldSetInterval(f,t); 
     return id; 
    }; 
    window.setInterval = newSetInterval; 
    //setTimeoutDeleted 
    function __log(msg) { 
     if (window.console && window.console.log) { 
      window.console.log(msg); 
     } 
    } 
}; 

var script = document.createElement('script'); 
script.setAttribute("type", "application/javascript"); 
script.textContent = '(' + go + ')(window);'; 
document.body.appendChild(script); // run the script 

當我添加

clearInterval(id); 

之前

return id;  

字面上未能對點擊響應的頁面,啓動「遊戲」。我接近這個錯誤嗎?我需要某種延遲,還是錯過了大局?

回答

1

您的問題是,有多個setInterval調用,看起來像3在我的一端。

如果您在點擊「開始遊戲」之前在控制檯中運行此代碼,它會將以下調用記錄到setInterval

var originalSetInterval = window.setInterval; 
window.setInterval = function(func, intr) { 
    var id = originalSetInterval.apply(window, arguments); 
    console.log('----setInterval----'); 
    console.log('function:', func); 
    console.log('interval:', intr); 
    console.log('id:', id); 
    console.log('-------------------'); 
    return id; 
}; 

然後,當你點擊「開始遊戲」,你會得到如下輸出。

----setInterval---- 
function: function() 
interval: 17 
id: 10 
------------------- 
----setInterval---- 
function: function() 
interval: 17 
id: 12 
------------------- 
----setInterval---- 
function: function() 
interval: 100 
id: 13 
------------------- 

隨意停止閱讀這裏繼續閱讀之前,做一些自己的實驗。

您可能不想在所有這些上調用clearInterval。運行時鐘的那個似乎是間隔時間的那個。要禁用該間隔而不觸及其他間隔,可以使用簡單的if語句。

var originalSetInterval = window.setInterval; 
window.setInterval = function(func, intr) { 
    var id = originalSetInterval.apply(window, arguments); 
    console.log('----setInterval----'); 
    console.log('function:', func); 
    console.log('interval:', intr); 
    console.log('id:', id); 
    console.log('-------------------'); 
    if (intr === 100) { 
     clearInterval(id); 
    } 
    return id; 
}; 

這樣做會成功停止時鐘。然而,一旦你完成遊戲,你會發現遊戲仍然知道你花了多長時間。時鐘只是一個視覺元素。

如果您想欺騙遊戲,您需要定位實際計算最終分數的代碼。這聽起來像是學習如何使用瀏覽器的開發工具,尤其是JavaScript調試器(使用漂亮打印功能使縮小的JS更易於閱讀)的絕佳機會。

+0

等待。所以這些都沒有改變服務器端。哇,好的。那麼你可以指點我一些資源來學習JavaScript調試器的東西嗎? – 2015-02-11 19:05:17

+0

@JoeBob Mozilla爲Firefox提供了[一些很好的教程](https://developer.mozilla.org/en-US/docs/Tools/Debugger)。谷歌還爲Chrome提供了[一些教程](https://developer.chrome.com/devtools)。 – 2015-02-11 19:09:16

+0

Theres還承諾用cookie,因爲管理員自己暗指:「key:scatter- {ID OF QUIZ} value:記錄時間的十分之幾秒 domain:quizlet。com「 - 將這個工作嗎? – 2015-02-11 19:14:07