Wordpress Themes

A place for my programming projects and the occasional blog about technology related matters.

Mozilla Extension Generator

Author einar

Since I started creating Mozilla extensions I spent a lot of time writing boilerplate code, and copying from one extension from the next. Then I found Ted Mielczarek’s Extension Generator which is a great page that takes some parameters and creates an extension ready for you to use. I used that for some time but in the end I still kept modifying each extension to fit my own style and include my library functions. So I decided to make my own Mozilla Extension Generator in Python, that would spit out extensions exactly the way I want them. I mostly liked the output of Ted’s generator so I took an extension from it, modified to fit my style and then created the generator to create it. So if you see similarities between extension my by generator and Ted’s, that’s the reason, mine is partially based on his output. I’ve decided to put the generator on my page for others to download, and they can then tweak it to fit their ideas of what should be in all extensions.

When the script is run the output will look something like this:

Mozilla Extension Generator
Copyright (c) 2007 Einar Egilsson (http://tech.einaregilsson.com)

Extension name: My New Extension
Extension code name: MyNewExtension
Extension description: Does this and that
Firefox or Thunderbird (f/t) : f
Include Menu item ? (y/n): y
Include Context menu ? (y/n): y
Author name: Einar Egilsson
Guid: mynewextension@einaregilsson.com
Menu item label: My New MenuItem
Menu item access key: m
Contextmenu item label: My New ContextItem
Contextmenu item access key: c

Creating files...

mynewextension/install.rdf
mynewextension/chrome.manifest
mynewextension/defaults/preferences/mynewextension.js
mynewextension/chrome/content/mynewextension.js
mynewextension/chrome/content/overlay.xul
mynewextension/chrome/skin/overlay.css
mynewextension/chrome/content/mynewextension.png
mynewextension/chrome/content/mynewextensionlib.js
mynewextension/chrome/locale/en-US/mynewextension.dtd
mynewextension/chrome/locale/en-US/mynewextension.properties

Extension complete

This will generate all the necessary files for the extension, you can simply go into the folder created, zip it up, name as .xpi and install the extension. The questions are pretty straight forward, just about the only thing you need to know is that Extension code name must be a single word, because it will become a javascript object that all your other code will be wrapped in, to avoid polluting the global namespace.

Now, some features of the generated extension:

  • Localization ready, strings are in .dtd and .properties files
  • Includes a library file with common function like accessing your prefs, writing to a file and more.
  • Includes debug functions that output to the javascript console and can be turned off in about:config
  • Can include menu item and context menu commands
  • …and more, just look at the generated files

The generator is a single python file that includes all the other files. It should be easy to modify for anyone with a working knowledge of python. There are a few constants at the top that you can tweak, they are:

#Constants
#Fill these out so you won't get prompted for them everytime:
YOUR_NAME       = ''
YOUR_DOMAIN     = '' # if filled out the extension guid will be of
                     #the form extensionname@yourdomain.com
FIREFOX_MIN     = '2.0'
FIREFOX_MAX     = '2.0.0.*'
THUNDERBIRD_MIN = '2.0'
THUNDERBIRD_MAX = '2.0.0.*'
INITIAL_VERSION = '0.9'

The icon file is also embedded in the file as a base64 encoded string. To replace it with your own file, create a .png image file (34×34 pixels) and run the script below (download) to create the base64 string, then copy it over the existing base64 string in the extension.py file.

import sys, base64

if len(sys.argv) == 1:
    print 'Usage: base64 <filename>'
    sys.exit(0)

b64 = base64.b64encode(open(sys.argv[1], 'rb').read())
i = 0

while i < len(b64):
    print b64[i:i+80]
    i += 80

And finally, you can download the python script here. You can also view it syntax highlighted here but don’t copy it from there as the syntax highlighting might screw something up. Enjoy :)

CommentComment

For spam detection purposes, please copy the number 1104 to the field below: