Inspect Javascript Timers Greasemonkey Script

While I was debugging some javascript in the Chrome console, I came up with a Greasemonkey/Userscript that will dump all active javascript timers to the console as they are created. This includes timers that are set with setTimeout and setInterval.

The only downside is that depending on when your browser runs the script, it may not log all of the timers that are created before this script is executed.

// ==UserScript==
// @name          Inspect Javascript Timers
// @namespace     http://jazzychad.net/
// @description   Logs information about js setTimeout and setInterval calls.
// @include       *
// ==/UserScript==

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;
    
    var oldSetTimeout = window.setTimeout;
    var newSetTimeout = function (f,t) {
        __log("INSPECT_TIMERS: setTimeout - " + t + "ms");
        __log("INSPECT_TIMERS: " + f);
        var id = oldSetTimeout(f,t);
        return id;
    };
    window.setTimeout = newSetTimeout;
    
    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

The code is available over on Github, or you can install directly from userscripts.org.

Happy timer hunting!

\0
You've just read a Bit. Read more Bits here.
Tagged: javascript code

blog comments powered by Disqus