September 3, 2005

Scripted loading of XUL overlays

When I switched from SeaMonkey to XULRunner as a basis for developing Verbosio, I hit a snag: certain chrome overlays didn't exist. For that matter, certain chrome packages, such as communicator, didn't exist. That was not a happy discovery.

Fortunately, the crew at #developers on irc.mozilla.org had a solution I didn't know about: scripted overlay loading. It's really simple:

window.addEventListener("load", function foo() {
  document.loadOverlay("chrome://verbosio/content/verbosioOverlay.xul", null);
}, true);

You have to do this after the document has finished loading; otherwise, you get a couple nice assertions and a broken application.

I can already see this having a couple of good uses. One is sniffing navigator.userAgent to determine if you're in SeaMonkey. (I know, I know, that can be overridden. For an alternative, #developers, gavin namely, suggested nsIXULAppInfo.)

if (navigator.userAgent.indexOf("SeaMonkey") != -1) {
  document.loadOverlay("chrome://communicator/content/tasksOverlay.xul", null);
} else {
  document.loadOverlay("chrome://tasks/content/tasksOverlay.xul", null);
}

I pass null as the second argument for two reasons. One, these overlays (unlike custom ones) are pretty much guaranteed to be there. So they'll load. That's the point of user-agent checking in this case. Two, the API is guaranteed in the IDL comments to change, and I'm not inclined to support two different versions...

Another option is loading overlays based on specific values of window.arguments. If I open a Verbosio window with an argument of "demo", I can turn off normal editing overlays and apply overlays specific to the demo mode of Verbosio. Or I can use the user's preferences to determine which combination of Verbosio extensions he wants applied.

Posted by WeirdAl at September 3, 2005 3:21 PM
Comments

I have a branding.js file in my extensions that support release & trunk builds for finding the Brand Short Name and other key names that will help in detecting multiple Mozilla applications. You can currently find it in my Local Install extension, enjoy.

Posted by: Mel Reyes at September 3, 2005 9:07 PM

It's just a shame something as useful as this was not added until February 2005. :-(

Posted by: James Ross at September 4, 2005 1:40 PM