Making an OpenStreetMap handler for Operator

From rukapedia
Jump to: navigation, search

The Operator extension for Firefox is a nifty tool for exploring the nascent world of microformats.

After adding some microformats to my website my next task was to try to develop a new "handler" for the extension, something that's alluded to here and explained more fully here.

My goal was to create a handler that would open up a geo location in the OpenStreetMap map in the same way that the default Operator setup opens maps in Google and Yahoo maps.

Looking Under The Hood of Operator

I'm new the world of Firefox extensions and "jar" files and the like, so it took me a little while to figure out how to take a look under the hood of Operator:

  • Navigate to your Firefox Profile directory; on my Mac this was under:
~/Library/Application Support/Firefox/Profiles
  • In the extensions directory, look inside the directory somewhat confusingly named:
  • Make a copy of the file called operator.jar you find there, but call the copy
  • Use your favourite unzipping utility to "unzip" the file.
  • The result will be Operator in all its glory; the heart of the matter seems to be microformats.js


Making an OpenStreetMap Handler

Inside the Operator directory (the one inside extensions, not the one you just unzipped) is a sample handler called handler-example.js that's a good place to start. I started there, and ended up with this, which I saved as handler-osm.js in a new microformats directory in my main Firefox Profile directory:

if (typeof Microformats == "undefined") {
  var promptService = Components.classes[";1"].
  promptService.alert(window, "", "Microformats are not available. Microformats.js must be included");

/* you must specify the handler you are adding to here, as well as the microformat */
/* it applies to */
Microformats.geo.handlers.osm = {
/* Specify the description */
  description: "OpenStreetMap",
/* specify an encoded icon */
  icon: "data:image/png;base64," +

/* The action will be called whenever your item is selected */
/* We have provided a few helper function to be used in action */
/*   Microformats.loadUrl(url); - loads a URL */
/*   bookmark(item, microformatname) - bookmarks the microformat */
  action: function(item, event)
    var geo = Microformats.geo.create(item); 
    Microformats.loadUrl("" + geo.latitude + "&lon=" + geo.longitude + "&zoom=16", event);

Things that I changed from the example are:

  • became Microformats.geo.handlers.osm -- because I'm creating a handler for the geo microformat, not the hCard used in the example.
  • description: "Bar!", became description: "OpenStreetMap", -- this is the description that Operator uses to display the name of the handler.
  • the string appended to "data:image/png;base64," + became a base64-encoded version of the OpenStreetMap icon that I created by taking a 16x16 PNG of the icon and running it through uuencode:
uuencode -m osm.png osm.png
  • and, finally, the action became:
var geo = Microformats.geo.create(item); 
Microformats.loadUrl("" + geo.latitude + "&lon=" + geo.longitude + "&zoom=16", event);

I figured the first line out by looking at the geo.js file inside the microformats directory inside operator.jar.

Installing the new Handler

Once the new handler-osm.js is installed (and, I think, Firefox restarted), activating the new handler is as simple as opening up the Operator Options dialog, selecting the Actions tab and creating a new action, as illustrated below:


The result is that when I visit a page with geo microformatted information embedded in it, my Operator toolbar looks like this: