667
edits
Changes
no edit summary
Extensions allow third-party developers (and Mozilla, for that matter) to write add-on packages that users can install to extend or modify the standard browser. By rewriting our earlier code as an extension, we can give users an extension to install, which will have the same effect as our custom build. For all but the most universal of changes, extensions are the best way for developers to write code that targets the browser.
==Planning the extension==
Having decided to rewrite the code as an extension and remove our changes from the tree, a logical first thought would be: "how can I modify my existing code to create an extension." While we learned some useful things modifying the browser directly, our extension will require a completely new approach to solving the same problem.
var t = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "tab");
...
this.mTabContainer.appendChild(t);
...
// Dispatch a new tab notification. We do this once we're
// entirely done, so that things are in a consistent state
// even if the event listener opens or closes tabs.
var evt = document.createEvent("Events");
evt.initEvent("TabOpen", true, false);
t.dispatchEvent(evt);
return t;
After the tab is created and appended to the end of the list, an '''event''' is '''dispatched'''. The developers of tabbrowser.xml wanted their code to be extensible; that is, they wanted to provide other developers a hook on which to hang their code. Their foresight makes our job much easier.
Now we have a The name of the event is significant, as we'll have to write co'''TabOpen''', which is important for us.
Start by creating a new extension, either by hand, or using Ted Mielczarek's wonderful wizard: