mk_ext.sh - Making a Firefox extension the quick & dirty way
Sometimes it’s nice to be able to create a separate extension that you install into a Firefox profile, to test things out quickly, or to experiment with some new functionality. But a simple HTML file won’t do, either because you need chrome privileges or you need to access XUL elements or XPCOM components. There’s a lot of boilerplate code that is involved setting up the structure of a Firefox extension, though. Normally when I need to do this I spend a few minutes copying and pasting things from some other extension I’ve written that still happens to be lying around. But years ago I realized "I should just write a script to do that" and unlike the other five thousand times I’ve said that, this time I’ve actually done it.
#!/bin/bash
#
# Copyright 2009 Michael G. Collins.
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
if [ -n "$1" ]
then
EXTENSION_NAME=$1
else
echo "You must provide a name for the new extension."
exit
fi
#get a guid
UUID=`uuidgen | tr A-Z a-z`
echo "Creating extension $EXTENSION_NAME with ID $UUID in directory `pwd`/$EXTENSION_NAME"
#make dirs
mkdir -p "$EXTENSION_NAME/chrome/content/$EXTENSION_NAME"
#create a skeleton install.rdf
cat > "$EXTENSION_NAME/install.rdf" <<EOF
<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>{$UUID}</em:id>
<em:name>$EXTENSION_NAME</em:name>
<em:type>2</em:type>
<em:version>0.1</em:version>
<em:creator>mk_ext.sh</em:creator>
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>1.5</em:minVersion>
<em:maxVersion>3.0.*</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
</RDF>
EOF
#create a basic chrome.manifest
cat > "$EXTENSION_NAME/chrome.manifest" <<EOF
content $EXTENSION_NAME chrome/content/$EXTENSION_NAME/
overlay chrome://browser/content/browser.xul chrome://$EXTENSION_NAME/content/$EXTENSION_NAME.xul
EOF
#create a stub overlay XUL file
cat > "$EXTENSION_NAME/chrome/content/$EXTENSION_NAME/$EXTENSION_NAME.xul" <<EOF
<?xml version="1.0"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="chrome://$EXTENSION_NAME/content/$EXTENSION_NAME.js"/>
</overlay>
EOF
#create a JS file
cat > "$EXTENSION_NAME/chrome/content/$EXTENSION_NAME/$EXTENSION_NAME.js" <<EOF
const Cc = Components.classes;
const Ci = Components.interfaces;
EOF
echo "Done."
echo "You can install this into a Firefox profile with:"
echo "echo \"`pwd`/$EXTENSION_NAME\" > \"extensions/{$UUID}\""
exit
If this is something you’re looking for, and you haven’t already copied and pasted this code into a file, you can download it here (but really why haven’t you just copied & pasted already?). It’s a Bash script and I’ve tested in bash shells on Mac OSX, Linux, and FreeBSD, but I think it should work on any system which has bash and uuidgen installed (If anyone wants to try it out on cygwin let me know if it works for you).
To get started go ahead and create a new Firefox profile as described here or here.
Then just find the directory you created your new profile in, and copy the script over there. On Mac OSX, the default is /Users/<user_name>/Library/Application Support/Firefox/Profiles, on *NIX it’s something like ~/.mozilla/Firefox/Profiles/ and on Windows, well, who cares they can figure it out for themselves, I don’t use Windows.
Run the script with something like $sh mk_ext.sh my_new_extension with the only argument being the name you want to use for the new extension. The script will generate a unique ID for the new extension as well as the chrome manifest and install RDF file and a very simple XUL and JS file (using the name you supplied on the command line) for you, and will output the ID for you as well as a simple "echo" command that will create a pointer for the newly created extension directories in whatever profile you choose to run it in. If your still sitting in the directory you ran the script in, just copy that command and run it. Fire up the Firefox profile you installed the new extension into, and start hacking!
Trackbacks
Use the following link to trackback from your own site:
http://blog.collinsmichaelg.me/trackbacks?article_id=15