Friday 26 September 2008

My second firefox extension

Once I had a development problem and googled for a solution.
I found a page were my problem was exactly described but the solution was hidden. It was only available for registered users. Registration was not free.

So I asked my boss to get an account for this service.
In the meanwhile a colleague of mine found another solution. He checked whether the page containing the solution was in the google cache and so it was. Probably google has an account which is used by the google crawler.

Searching for a firefox extension which load the google cache for a given page I found nothing working for firefox 3, so I started an extension myself.

The extension wizard completed most of the work. It defined an entry in the context menu and the only work left was to implement the functionality behind the context menu entry. After some searching on the mozilla developer page I found the necessary snippets. This is the resulting function:

onMenuItemCommand: function(e) {
/*
* get the recently used browser window
* http://developer.mozilla.org/en/Code_snippets/Tabbed_browser
*/
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
getService(Components.interfaces.nsIWindowMediator);
var recentWindow = wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
var googleCacheUrl = "http://google.com/search?q=cache:"
+recentWindow.content.document.location;
/* open the url in a new current tab */
gBrowser.selectedTab = gBrowser.addTab(googleCacheUrl);
}
}


That's all.

The source code is available via anonymous svn with

svn co http://subversion.banapple.de/public/getgooglecache/


The extension can be installed from www.banapple.de.

Meanwhile I found an old extension which seems to do the same. It only works for Firefox 1.5.

No comments: