Born Geek

Chapter 7: Packaging the Toolbar

Up to this point in the tutorial, we have been relying on the dynamic development system to handle installation responsibility for our extension. The end-user doesn’t want to go to all of this trouble to install your extension, so we must package everything up into a cross platform installer (XPI) file. Don’t let the file extension fool you; this file is simply a zip file in disguise.

This is where our zip tool comes in handy. For the purposes of this tutorial, I will make use of the 7-Zip command line tool.

No More JARs

Once upon a time, Firefox required that extensions package the files within their content folder in a JAR file, which would then be packaged within an XPI. This practice is no longer required in modern versions of Firefox. A flat file structure is all we need, making our lives much easier. Gone is the requirement to update the paths in our chrome manifest, pointing to a location within the JAR file. Also gone is the tricky zip commands needed to include just the right files in the outer XPI.

Creating the XPI File

The XPI file is what you will actually distribute to the end-user. We will create this file in the top-level folder of our extension’s file structure. Once the file is created, our file structure will look like the following:

TutToolbar/
 |-- tuttoolbar.xpi
 |-- install.rdf
 |-- chrome.manifest
 +-- chrome/
      |-- content/
      |    |-- tuttoolbar.xul
      |    +-- tuttoolbar.js
      +-- skin/
           |-- image_sheet.png
           |-- gripper.png
           +-- tuttoolbar.css

The XPI file must retain relative path information, which can be handled via the appropriate call to our zip tool. To create the XPI file using the 7-Zip tool, change to the top-level folder of your extension’s file hierarchy (the same folder where the installer and chrome manifests live), and use the following command (this assumes that the 7-Zip executable is in your system PATH environment variable):

7z.exe a -tzip tuttoolbar.xpi *

Every file and folder at and below the current directory will be placed into a zip file named tuttoolbar.xpi, which is exactly what we want in our example case. You can specify specific files and folders to include, should you desire to omit certain ones from the package.

Test Installing Your Extension

Now that we have an XPI file, we can test out the installation process. This is something you should always do before you release an extension to the public: make sure it installs! We must not use our development profile to do installation testing, since we already have an "installed" version of our extension in that profile. In either your normal Firefox profile, or in yet another test profile, take the following action to install your extension:

  1. Select the File » Open File… menu item (or press the Ctrl+O keyboard accelerator).
  2. In the file selection dialog, browse to the location of your XPI file, select it, and click the Open button.

If you’ve done everything correctly, the extension installer should appear. Tell it that you want to install, close Firefox when it’s finished installing, and start Firefox back up. Your toolbar should appear alongside all of the others. If your toolbar doesn’t appear, it’s likely that you either have a bug in your zip file (perhaps you forgot to use the -r option), or there’s a bug in your chrome manifest. We’ll take a look at some debugging methods in the next chapter.

Speeding Up the Packaging Process

Packaging your extension by hand can quickly become tiresome. Why not write a script to do it? If you have a zip tool with a command line interface, you can easily automate the process. The following example shows a simple way to build our example toolbar using a Windows batch file and the 7-Zip tool.

Create a DOS batch file (a text file with a .bat file extension) in the extension’s top-level folder. The code I used in the batch file is shown below:

@echo off
if exist tuttoolbar.xpi del /F tuttoolbar.xpi
7z.exe a -tzip tuttoolbar.xpi *

As you can see, this script is extremely simple. All it does is removes any preexisting XPI file, and then recreates it using our zip tool. Your scripts can be much more complex than this, however. The Perl script I use to build Googlebar Lite does a number of things: it automatically updates all the version numbers for me, allows me to specify the output filename, and packages the files. For the curious, I’ve provided the script for you to look at. [View the Googlebar Lite Build Script]

| Table of Contents |