mk_ext.sh - Making a Firefox extension the quick & dirty way

Posted by mcollins Sat, 07 Feb 2009 22:49:00 GMT

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!

A New Host

Posted by mcollins Sun, 18 Jan 2009 03:57:00 GMT

Last week I quietly moved this blog along with everything else on my old server to a new host.  Why, you ask?  Well cost was a big factor, I simply felt that I was paying too much for way more server than I actually needed.  But another equally important factor was the server OS.  I moved from RHEL4 to FreeBSD 7, and I am glad that I did.  I’ve been running various flavors of Linux on desktops and servers for over 10 years, and have only toyed with FreeBSD occasionally, but I’ve always wanted to find an excuse to start using it more.

Installing all the software I needed was a breeze thanks to the ports collection, and overall the administration has just been a lot less painful.  My new hosting provider, RootBSD, were really quick getting everything setup, and their support (though I haven’t needed anything from them besides getting my domains on their DNS) were really fast and helpful.

I also feel a little better about supporting BSD-licensed software over GPL, but I am going to resist the temptation to turn this into a "You got your politics in my software!" rant.  I’m just going to run the software that works best for my needs, and I plan to continue running Slackware on my desktop at home for a long time.

The iPhone Horror (part 3) - back from the dead

Posted by mcollins Sun, 18 Jan 2009 03:47:00 GMT

After my unsuccessful attempt at open-iPhone surgery I was in a great deal of shock.  Obviously the thought of having to live without the comforts the iPhone provides can be unsettling.  Even more unsettling, though, is the thought of having to buy another iPhone without the benefit of the carrier’s subsidy.  There was some hope however, as the iPhone was still showing a few signs of life.  So I decided to purchase the combined LCD screen and digitizer ( that’s the touch screen ) that comes pre-assembled from the same site I ordered the original touch screen from.  This turned out to be a much better option, and with the benefit of hindsight I can see clearly that this is what I should have done from the start.

With the pre-assembled LCD and digitizer, replacing the screen is as simple as popping the old one out and sticking the new one in.  It did take a couple of reboots after I swapped the screens, but this may have been due to the fact that I had left the iphone sitting unplugged for about a week after I had so brutally disemboweled it, while I waited for the new screen to arrive.  I think it may have needed a reboot to initialize the new hardware, but ended up in a funky state after it attempted to reboot and then went into dead battery hibernation mode.  Luckily with a full charge and a few more reboots, my iPhone was back up and running just like before.  Well, to be honest, it’s mostly working like it was, but with a few minor quirks that I think were caused by collateral damage, such as the vibrator not working.  I think it was still working after I dropped the phone, which would mean that I probably knocked a wire loose that I forgot to reconnect.  Or it could have been broken the whole time - it’s possible I just overlooked it as I was more concerned with just getting the screen working again.  Either way, it’s something I can easily live without.

And lucky for me, my sister got me a case for my iPhone for Christmas.  It’s one of those clear plastic touch-thru types (if I could remember the brand, I’d mention it, but I forget which one it was), that completely surronds the phone, with cutouts for the buttons and ports.  I really like it, it looks good and doesn’t feel like it effects the accuracy or responsiveness of the touch screen at all.  I had held off on getting a case for my iPhone because I was concerned about the added bulk, but after your unprotected iPhone lands face down on the pavement that ceases to be an important concern.  After using it for several weeks with the case on, I’ve become so accustomed to it, that when I had taken the iPhone out of the case the other day, it felt awkward in my hand, like it was too small now.  It also seems like it might add a good degree of protection from impacts, but I’m not about to chuck it onto the sidewalk again just to test my hunch.