How it works: Hide Toolbars in Firefox 4

javascriptxul

Last week I finished my HideToolbarsByURL extension. Now I want to explain a little bit how it works. Because I have used this technique in my other project I thought this could probably be usefull for other people as well.

Hide the Toolbar

In Firefox 4 there was a new Addon-Manager introduced which opens itself in a new tab. If you have set your tabbar to be on top of the navigation bar this addons-manager tab has no navigationbar or bookmarksbar. This is realized with the new function hideChromeForLocation of the XULBrowserWindow object. This function is called everytime a tab is shown e.g. when you open a new tab or you switch to another tab. This function gets the current location (URL) of the tab as an argument of the type string. If this function returns true for the location then the toolbars are hidden.

If you want to hide your URL too you have to override this function so it returns true for your location. But if you simply replace this function with your own, other extensions and firefox itself wouldn't be able to use this feature anymore. The recommended way to get your location hidden is to chain your implementation of this function with the original one.

First you save a reference of the original implementation. Then you can override the function with your own. You have to determine if the given location is your desired location and return true. Otherwise call the original implementation.

var old = XULBrowserWindow.hideChromeForLocation

//override the hideChrome funtion with our logic
XULBrowserWindow.hideChromeForLocation = function (aLocation) {
  if (0 === aLocation.indexOf("http://www.lestard.eu")) {
    return true
  }
  //Call the original hideChrome-function
  return old.call(XULBrowserWindow, aLocation)
}

If the current location (aLocation) starts with "http://www.lestard.eu" then return true and hide the Toolbars. Otherwise call the old implementation with the aLocation as argument.

In my extension I put the URLs which should be hidden in an array. In my overridden hideChromeForLocation function I look if the current Location is in that array or not. Because I only want to check the first part of the URL (the domain) I use the nsIURI object from mozilla.