Home > Uncategorized > A Firefox addon to define

A Firefox addon to define

Greetings! It feels great to write my first tech-post on the first day of the year! Have you ever thought of developing an addon for a browser as a geeky task? Well, thats how I was thinking! But, thanks to an inspiring chat with this guy, here I am with an addon for my dear Firefox πŸ™‚

Many of us (including me) frequently use “define:” type of search at Google, to lookup definitions of words, that we come across while browsing. Each time then, I had to type “define:” at Google search bar and copy-paste the word (to be defined), which was quite tedious. Yes! I have found what my addon should do. Its objective would be to define the selected text without typing “define:” keyword. But.. What if there is a word outside the browser (may be a pdf, doc) that needs to be defined? So, the addon should also be able to define those words which aren’t present in webpages, again without typing the “define:” keyword.

Although a simple JavaSript code might do the above, I found this an interesting task for my first addon. If there is Google, can Microsoft be behind? No! So, the addon would search Bing too (though not all versions of Bing support “define:” type of searches). As a perk, normal search is also provided.

If you are reading this blog using Firefox, then here is my New Year’s gift for you 8)
To install the DefineIT addon, go here. Source code is available at the addon homepage.

Now, lets delve into the technical stuff. Basically, XPCOM, XUL, JavaScript, CSS are the technologies which govern Firefox and its addons. For a simple addon like this, there was no need to deal with XPCOM. XUL is Xml based User interface Language, used for designing the UI part.Β  JavaScript, as you expected, implements the desired funtionality of an addon. CSS takes care of decorating the addon.

Here is a code to greet you, using XUL and JavaScript πŸ™‚

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" title="Sample to demonstrate XUL and JavaScript">
 <button label="Click me!" onclick="wish()"/>
 <script>
 function wish()
 {
window.alert( "Wish you a happy new year!!" );
 }
 </script>
</window>

Just copy the code above and save it as “greetings.xul” or any name with “.xul” extension. Now drag-drop the file into Firefox (in a new tab). You can see a button, which on clicking greets you.

One look at the code above, you will realise that XUL is nothing but a mix of DHTML and XML elements. If you have basic knowledge of DHTML, XML, JavaScript, then you are just few hours away from your own addon! Mozilla has a nice documentation here.

Overlaying is the most interesting feature of XUL, whereby you lay a XUL file over another XUL file. The Firefox browser is itself a XUL file. So, it makes sense that we have to lay the XUL files of our addon over the XUL file of Firefox, to make our addon work.

Now, lets look behind the screens of addons. Users of Firefox would be familiar with the XPI file format of its addons. XPI is an archive format like ZIP. You can rename any .xpi file as .zip and view its contents. Essentially every XPI file, when extracted, contains the following files:

  • install.rdf
  • chrome.manifest
  • chrome folder

install.rdf has the basic information required to install any addon, right from the version of addon to checking compatibility with different versions of Firefox.

chrome.manifest is used to register the chrome packages with Firefox.

chrome folder contains 3 packages, namely

  1. content — contains all the XUL and JavaScript files.
  2. locale — contains the textual part of addon, which needs to be translated into various languages, since Firefox speaks many languages!
  3. skin — contains the CSS files.

The Addon Builder helps you create the files and folders mentioned above. It provides a template sort of thing, so that you need not code from the scratch, which means you can spend more time on the functionality of your addon.

And, in case you are wondering who will translate your addon into other languages, BabelZilla isΒ  the answer. You submit your addon in your own language to them. Translators all over the world translate your addon into other languages and give the localized addon back to you! (By the way, I haven’t done localization for DefineIT. So, you can see only the en-US folder inside locale package.)

In case of DefineIT, all I had to do was:

Normal search is same, minus the “define:” keyword in the URL. Similar thing for using Bing.

Simple! Isn’t it? Now that I have spoken enough, its time to hear from you! Comments, suggestions, bugs, feature requests, etc.. Pour them all below this post πŸ™‚
And, wish you all a happy, healthy and prosperous new year!

  1. Vivek
    January 1, 2010 at 2:29 PM

    By far the only AddOn that I use most frequently in Firefox! πŸ™‚

  2. January 1, 2010 at 10:39 PM

    An extremely cool venture! This addon is really awesome! Starting a blog with an information as great as this is a perfect new year gift for all of us, indeed! I have no doubt that you will keep posting many more great entries that will inspire and enlighten a lot of people! Keep Rocking as always!!! πŸ™‚

  3. Arun
    January 2, 2010 at 2:42 PM

    Very good! Go on πŸ™‚

  4. Rajesh V
    January 2, 2010 at 11:09 PM

    Great work! Keep rocking!

  5. Anjali R
    January 4, 2010 at 4:08 PM

    Amazing work!! cool!

  6. Srivathsan
    January 5, 2010 at 9:55 PM

    Its of great use for me. Would be better if i can have shortcut for this.

    • January 5, 2010 at 10:20 PM

      Will add it ASAP. Thanks for the suggestion πŸ™‚

  7. Khaja
    January 11, 2010 at 3:15 PM

    Good work.. It would be really helpful for me..

  1. No trackbacks yet.

Leave a reply to Srivathsan Cancel reply