Posts Tagged ‘tutorials’

Video Tutorial – Extensions Bootcamp: Zero to “Hello World” in 45 Minutes

Wednesday, March 18th, 2009

Mozilla’s Myk Melez created a great video introduction to add-on development for Mozilla’s Design Challenge Spring 09. If you’ve be looking for a video walk-through, Myk just served it up for you.

Grok extensions, set up your development environment, and make your first one. We’ll explain how extensions integrate into Firefox and what they can do, show you how to set up an environment to ease their development, and walk you through the making of a simple “Hello World!” extension. Bring your laptop and prepare to follow along. By the end of this session, you’ll be an extension developer.


Extension Bootcamp (Mozilla Labs Design Challenge: Spring 09) from Mozilla Labs – Concept Series on Vimeo.

Now combine the video with Robert Nyman’s step-by-step add-on tutorial and the newly released Firefox Add-ons Developer Guide and you’ll be well on your way to honing your add-on development skills.

Firefox Add-ons Developer Guide (beta release) – Calling all Add-on Developers!

Wednesday, March 11th, 2009

Summary: The Firefox Add-ons Developer Guide is  now released in beta.  We are now  searching  for  contributors,  if  you want  to  help,  please  see instructions below.

Firefox Add-ons Developer Guide – beta release

It’s with  great excitement that we  announce today the beta  release of the Firefox Add-ons Developer Guide. This  guide, is based on an earlier tutorial written and printed for  an Add-ons conference organized by our colleagues in Japan back in June 2007,  and has now been updated for the Firefox  3.5  release.  The  document  will  guide  and  assist  add-ons aficionados around the world, eager to develop their own Firefox add-on. Its catered  to all types  of user,  from the experienced  developer who needs a little push in the right  direction, to the new beginner keen to get his hands dirty, but not sure where to begin.

The content of the guide

The guide is divided into 6 chapters and one appendix:

Top page

  1. Introduction to extensions
  2. Technologies used in developing extensions
  3. Introduction to XUL: How to build a more intuitive UI
  4. Using XPCOM: Implementing advanced processes
  5. Let’s build a Firefox extension
  6. Firefox extensions and XUL applications
  7. Appendix: What you should know about open-source software licenses

State of the document

As  described, this  is a  beta  version of  the document.  The text  of the  guide has  been  thoroughly revised,  and is  now  open to  further contributions, corrections  and modifications. Paul Rouget  of Mozilla’s evangelism  team  is coordinating  the  project  and will  be  regularly reviewing  and integrating  contributions from  the community.  Feedback will be incorporated, ready for final release a few weeks before Firefox 3.5.

How to contribute

If you  would like to  help, first, please  read through the  task list. The guide is hosted on MDC, which is a wiki, so feel free to add content and fix mistakes. If you would  like to provide comments and suggestion, or if you want to ask questions, please do so through the MDC forum.

If you would like to propose a deep modification, please, first, discuss it via the forum.

Thanks

Thanks a lot to the original authors:

  • Hideyuki Emura
  • Hiroshi “Piro” Shimoda
  • Taiga Gomibuchi
  • Taro Matsuzawa
  • Yutaka Kachi

… and Erwan Loisant, Andrew Williamson and Brian King for their useful reviews.

The license of the guide is Attribution-Share Alike 2.1 Japan.

Calling all Add-on Developers!

Mozilla Add-ons developers out there, if  you would like to help improve

this guide  and make it become  “the” reference for those  interested in developing a Firefox  add-on, take a peek  at the guide, drop  us a line and tell us  what you would change, add, delete  or improve. The Firefox Add-on Developer Guide will always be a work in progress, so please help

improve it today !

How to develop a Firefox extension

Wednesday, January 28th, 2009

Reposted with permission from Robert Nyman:

Admit that you have always wanted to know how to develop a Firefox extension but never had the time to learn. :-) Here I will walk you through and at the end of the article we will have created a fully functional Firefox extension!

Our objective

We will create a Firefox extension to find all links in the current web page, highlight those which have a target attribute and alert you how many links it found. The good part is that once you have done this, you have both an understanding of Firefox extension development as well as a blueprint for any extension you would want to develop in the future.

(more…)

Firefox Extensions: Global Namespace Pollution

Friday, January 16th, 2009

Reposted with permission from Jan Odvarko (AKA Honza):

I have been recently asked by couple of developers how to properly design architecture of a Firefox extension. The first thing that immediately came to my mind at that point was a problem with global variables defined by extensions in ChromeWindow scope.

This problem can easily cause collisions among various extensions. Something that should be always avoided (and is also part of AMO review process) since this kind of issues is very hard to find. Yes, global variables are still evil, especially in OOP world.

I don’t want to describe how to develop a new extension from scratch. For this there is already bunch of detailed articles. I am rather concentrating on effective tactics how to make Firefox extension architecture maintainable and well designed.

So, read more if you are interested…

Namespace Architecture

Defining global variables is a way how to risk collisions with other extensions. I think that creating just one global variable per extension that is unique enough (composed e.g. from the name of the extension, domain URL, etc.) is sufficient strategy how to avoid undesirable collisions.

The architecture for namespaces used in Firebug, is based (more or less) on well known module pattern (originally described by Douglas Crockford). It’s really simple and transparent so, I hadn’t understand how it actually works for a long time. I believe other extension developers can utilize this approach as well.

The basic idea is to wrap content of every JS file into its own scope that is represented by a function so, there are no global objects. See following snippet.

function() {

// TODO: entire JS code in this file is here
}

This is what I am going to call a namespace.

The first question is how to ensure that the function is actually called and the code executed at the right time. The second question is how to share objects among more files (see Sharing among namespaces chapter below). Firebug solves this by registering every namespace and executing all when Firefox chrome UI is ready. See modified example.

myExtension.ns(function() {

// TODO: entire JS code in this file is here
});

The namespace (regular function) is passed as a parameter to myExtension.ns function. The myExtension object is the only global object that is defined by the extension. This is the singleton object that represents entire extension. Don’t worry if the name is long, there’ll be a shortcut for it (in real application it could be e.g. comSoftwareIsHardMyExtension).

The ns function is simple. Every function is pushed into an array.

var namespaces = [];

this.ns = function(fn) {
    var ns = {};

    namespaces.push(fn, ns);
    return ns;
};

Actual execution of registered namespaces (functions) is only matter of calling apply on them.

this.initialize = function() {
    for (var i=0; i<namespaces.length; i+=2) {

        var fn = namespaces[i];
        var ns = namespaces[i+1];

        fn.apply(ns);
    }
};

Now, let’s put all together and see how the global extension (singleton) object is defined and initialized.

The following source code snippet represents a browserOverlay.js file that is included into an overlay (browserOverlay.xul)

// The only global object for this extension.
var myExtension = {};

(function() {
// Registration
var namespaces = [];

this.ns = function(fn) {
    var ns = {};
    namespaces.push(fn, ns);

    return ns;
};
// Initialization
this.initialize = function() {

    for (var i=0; i<namespaces.length; i+=2) {
        var fn = namespaces[i];

        var ns = namespaces[i+1];
        fn.apply(ns);

    }

};
// Clean up
this.shutdown = function() {
    window.removeEventListener("load", myExtension.initialize, false);

    window.removeEventListener("unload", myExtension.shutdown, false);
};
// Register handlers to maintain extension life cycle.
window.addEventListener("load", myExtension.initialize, false);

window.addEventListener("unload", myExtension.shutdown, false);
}).apply(myExtension);

As I mentioned above, there is just one global object myExtension.

To summarize, the object implements following methods:

  • ns – register a new namespace.
  • initialize – initialize all namespaces.
  • shutdown – clean up.

And also, the code makes sure that initialize and shutdown methods are called at the right time. This is why event handlers are registered.

The browserOverlay.xul looks as follows now.

<?xml version="1.0"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <script src="chrome://namespace/content/browserOverlay.js" type="application/x-javascript"/>
    <script src="chrome://namespace/content/Module1.js" type="application/x-javascript"/>

    <script src="chrome://namespace/content/Module2.js" type="application/x-javascript"/>
</overlay>

And the Module1.js and Module2.js files are both the same.

myExtension.ns(function() {
// TODO: entire JS code in this file is here
});

Sharing among namespaces

Now, when we have our script within a local scope(s), let’s answer the question how to share functionality and data among individual namespaces. The general idea is to use the one global object we have – myExtension.

First of all, see the following source code (lib.js file).

myExtension.LIB = {

    // Shared APIs
    getCurrentURI: function() {
        return window.location.href;

    },

    // Extension singleton shortcut
    theApp: myExtension,

    // XPCOM shortcuts
    Cc: Components.classes,

    Ci: Components.interfaces,

    // Etc.
};

You can see that a new LIB property is created within our global myExtension singleton. This objects represents a library of functions that should be shared among all modules in our extension. At this point, you can also get inspiration from Java Packaging and create whole tree of namespaces within the global singleton (just like e.g. YUI does)

The lib.js file is included in browserOvelay.xul (just after browserOverlay.js)

<?xml version="1.0"?>

<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script src="chrome://myextension/content/browserOverlay.js" type="application/x-javascript"/>

    <script src="chrome://myextension/content/lib.js" type="application/x-javascript"/>
    <script src="chrome://myextension/content/Module1.js" type="application/x-javascript"/>

    <script src="chrome://myextension/content/Module2.js" type="application/x-javascript"/>
</overlay>

Let’s also improve our module script a bit.

myExtension.ns(function() { with (myExtension.LIB) {
// TODO: entire JS code in this file is here

var moduleVariable = "Accessible only from withing this module";

dump("myExtension.Module initialization " + getCurrentURI() + "\n");

}});

By utilizing with statement we can simply access all library functions as if they would be a global functions.

In case we want to access our singleton global object we can also utilize theApp shortcut (useful especially if the name is long) as follows:

myExtension.ns(function() { with (myExtension.LIB) {
// TODO: entire JS code in this file is here

theApp.sharedValue = "A new shared property";

}});

Here is how the architecture look like from UML perspective.

Namespaces within a Firefox extension.

Download example extension here.