// Internet Duct Tape

How to Profile Greasemonkey Scripts with Firebug

Posted in Firefox and Greasemonkey, Programming Tools, Technology by engtech on November 13, 2007

Programming Tips

Running performance analysis on Greasemonkey scripts can be a pain in the butt. They aren’t part of a webpage so standard tools for analyzing web sites don’t work… or do they?

The Goal

Profiling Greasemonkey scripts with Firebug

What You’ll Need

  1. Firefox
  2. Greasemonkey
  3. Firebug extension

The Trick

#1: You need to remove all of the Greasemonkey GM_* functions from the script you want to profile. This is easier than it sounds because all of the functions can be performed by plain ‘ole javascript (except for the open in new tab function and register menu command).

#2: You need to embed your Greasemonkey script inside of the running page so you can analyze it with Firebug’s profile tool. I have a function below that can embed a function inside the current web page.

#3: You’ll need to call the function either using unsafeWindow or by embedding a call to the function in the page.

#4: Litter your code with calls to Firebug’s console.profile() and console.time() functions.

Sample Code Template


(function() {
  function embedFunction(s) {
document.body.appendChild(document.createElement('script')).innerHTML =
s.toString().replace(/([\s\S]*?return;){2}([\s\S]*)}/,'$2');
 }

  function myKickassGreasemonkeyScript() {
    console.profile();
    // Put everything you need for your Greasemonkey script in here
    // Don't use any of the GM_* functions!

function kickass() {
      console.time("Block1");
      // Block of code that might take a lot of time
      console.time("Block2");
      // another block of code
      console.timeEnd("Block2");

      console.timeEnd("Block1");

    }

// more cowbell

console.profileEnd();
  }

  embedFunction(myKickassGreasemonkeyScript);
  // Method 1: embed the function call into the current page
  document.body.appendChild(document.createElement('script')).innerHTML = "myKickassGreasemonkeyScript();";
  // Method 2: directly call the function using unsafeWindow
//     window.addEventListener("load", function(e) {
//                   unsafeWindow.myKickassGreasemonkeyScript();
//                   this.removeEventListener('load',arguments.callee,false);
//                 }, false);

 })();

Firebug Tutorial

Michael Sync has a tutorial on using Firebug that describes the console.time() and console.profile() functions. The official website has a nice list of Firebug keyboard shortcuts and a brief description of all the console.* functions.

Related Posts

7 Responses

Subscribe to comments with RSS.

  1. [...] The best way to profile Javascript is with FireBug, but it doesn’t recognize Greasemonkey scripts unless you embed them in the page so FireBug can find them. Wikipedia has a list of profilers for popular [...]

  2. [...] How to Profile Greasemonkey Scripts with Firebug [...]

  3. [...] How to Profile Greasemonkey Scripts with Firebug [...]

  4. Shreevatsa said, on August 16, 2008 at 5:05 pm

    How can you do the equivalent of GM_setValue/GM_getValue in plain javascript?

  5. Sam said, on June 04, 2009 at 7:16 am

    Think the last line should be

    }());

  6. Piyush Soni said, on April 12, 2010 at 11:22 am

    Same question as Shreevatsa. You said every GM* method can be done by plain old javascript. I don’t know how to do that for GM_setValue and (more importantly GM_getValue). Any ideas on how to do it?

  7. Dan said, on July 04, 2010 at 12:44 pm

    You can replace the GM_* functions like this for example: http://userjs.org/scripts/browser/enhancements/aa-gm-functions

    It’s for Opera, but works in every other current browser, too.


Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 286 other followers

%d bloggers like this: