Changes

Jump to: navigation, search

Dive into Mozilla Modifying Firefox using an Extension Lab

1,874 bytes added, 10:46, 14 March 2007
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.
Instead of searching for existing code to change, we have to take the browser as a given and find ways to work with it. Here are some interesting pieces of To that end, let's look again at the code for [http://lxr.mozilla.org/seamonkey/source/toolkit/content/widgets/tabbrowser.xml#1122 tabbrowser.xml's addTab] method:
var t = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "tab");
return t;
After the new 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 a way for future developers to extend the code by inserting a hook on which . In this case, after a tab is created an opportunity is provided to hang their codedo ''something''. Their foresight makes our job much easier. ===How to move a tab?=== Now we know that there is a logical time/place to run our code via the '''TabOpen''' event. The next thing to consider is what our code will do. Previously we replaced '''append''' with '''insertBefore''' and stopped the problem before it happened. In this case, by the time the '''TabOpen''' event is dispatched, the tab will already be created and positioned at the end of the list. We need another solution, so we need to go hunting in tabbrowser's [http://developer.mozilla.org/en/docs/XUL:tabbrowser documentation] and [http://lxr.mozilla.org/seamonkey/source/toolkit/content/widgets/tabbrowser.xml code]. What we need is a way to reposition a tab after it has been created. A quick look through the docs for [http://developer.mozilla.org/en/docs/XUL:tabbrowser tabbrowser] reveals nothing. However, the [http://lxr.mozilla.org/seamonkey/source/toolkit/content/widgets/tabbrowser.xml code] is more helpful:  <method name="moveTabTo"> <parameter name="aTab"/> <parameter name="aIndex"/> ... The '''moveTabTo''' method takes a tab to be moved, and a new position. This is exactly what we need. Now there are just two things left to figure out: 1) how to get the newly inserted tab; and 2) the index of currently selected tab so we can go one to the right. ===How to get the newly created tab using code?=== Having already studied and worked with the code in '''addTab''', and knowing that new tabs are always appended to the end of the list, we could do this:  var container = gBrowser.tabContainer; var lastIndex = container.childNodes.length - 1; var newTab = container.childNodes[lastIndex]; However, looking back at the event dispatching code for '''TabOpen''' we see that the event is dispatched ??? t, where t is the newly created tab:  t.dispatchEvent(evt);   ===How to get the index of the currently selected tab?===    
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:

Navigation menu