Thursday, August 25, 2005

Debugging firefox (mozilla) extensions with venkman

In my quest to get OSX working the way I want, I discovered that a number of the firefox extensions that I use don't actually work on OSX. So I figured this would be a good opportunity to learn a little about mozilla extensions by debugging what was wrong with them.

In my quest to get OSX working the way I want, I discovered that a number of the firefox extensions that I use don't actually work on OSX. So I figured this would be a good opportunity to learn a little about mozilla extensions by debugging what was wrong with them.

First off, I recommend you create a separate dev profile so that you don't screw up your real profile while mucking around with debugging. There are also some other config variables documented at the mozillazine wiki, which are useful if you want to do stuff like editing extension files directly without having to restart firefox to have your changes take effect. To create a dev profile, I fired up the profile editor by running the command

/Applications/Firefox.app/Contents/MacOS/firefox-bin -P

Once the dev profile is created, run it and install venkman and the offending extension(s). I'm using venkman 0.9.85 on firefox 1.0.6.

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p dev

You should probably install just the extension you care about so that it can be debugged in isolation, unless of course, one extension is interfering with another and you need to figure out why.

Now you are set to debug your extension, but first configure venkman to be able to debug extension files by unchecking "Debug->Exclude Browser Files". You may also want to tell venkman to automatically break in javascript that causes an Error or Exception by checking the menu items "Debug->Error/Throw Triggers->Stop for Errors/Exceptions". Not all errors/exceptions are bugs, so this may cause more noise than its worth - especially if you have a bunch of extra extensions installed in your dev profile.

There are already a number of tutorials that teach one how to use venkman, so I won't go into that here. You should now be able to set breakpoints in your code and step through it whilst exercising your extension. However, if you want to be able to debug the initial load/startup of your plugin, you need to do a couple more steps.

First, relaunch firefox telling it to run just venkman, without starting a browser:

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p dev -venkman

At this point, since the browser window hasn't loaded, none of its extensions have been initialized either, so you can set a future breakpoint on the load of the extension's javascript file and be able to step through it while it is loading. In my case, I chose to debug the Single Window plugin because it seemed small and simple enough for me to get my feet wet. I also find it essential because new browser windows take a long time to start in my regular profile because of the extensions I have installed, with Google Toolbar being the biggest culprit.
/fbreak chrome://singlewindow/content/singlewindow.js 1

Now, its just a matter of telling venkman to start up a browser instance, which you can do with the open-dialog command, and then stepping through whatever code interests you.
/open-dialog chrome://browser/content/browser.xul

BTW, I submitted a fix for the Single Window extension to its author, and it should be in the next version > 1.4. If you can't wait, it was just a matter of saving the value of window.Startup at the point right before it was changed rather than saving it at initial extension load at which point it seems to be null on OSX:

Line 67: window.Startup = singwin.Startup;
becomes:
orig_Startup = window.Startup;
window.Startup = singwin.Startup;

5 comments:

  1. Hey! the first para repeats twice! correct it!

    ReplyDelete
  2. Venkman:

    Mozilla should really have put a BIG BUTTON on the Main Toolbar to choose whether "Browser Files" (XUL files) should be excluded.

    Or else at the first launch, show a 3-choice window.
    1. Only Webpage javascript
    2. Only Extension, Plugin & Firefox javascript (XUL files)
    3. All javascript

    ReplyDelete
  3. (contd)

    Because of the way Venkman is currently configured by default, some people would think that it CANNOT debug XUL files, instead of realizing that it is a dormant capability.


    Or simplest would be to have that Checkbox menu item for "Debug->Exclude Browser Files" to be TURNED OFF BY DEFAULT.

    If the simple Web Devs who just want to debug javascript for their websites DON'T want to see XUL files, they can go and manually CHECKMARK it.

    ReplyDelete
  4. r7k96u48wbf7jhn0

    ReplyDelete
  5. Hello, thanks a lot for the tips. I am still a novice with Firefox extensions. I tried to make Download Sort to work under Mac OS X to sort my downloads by date, e.g. ~/Downloads/2008.12.20 but when following your instructions I looks like the extensions isn't touched at all when downloading a file. It didn't break at a breakpoint i set at the beginning inside the extension.

    Download Sort can be found at:
    https://addons.mozilla.org/de/firefox/addon/25

    Maybe someone is having an idea how to make this work or can give me a hint how to solve it?

    ReplyDelete