Oct 12 2009

Getting the source window of a request

Category: Firefox, JavaScriptJonathan Fingland @ 3:35 pm

This post draws heavily on a question and answer on “An observer for URL changes (Firefox extension)” from StackOverflow.

In this post, however, I’m going to focus only on getting the window from which an http request originated.

  • What you need to know

    Yesterday’s post about XHR Listening by a Firefox Addon gives a good basis to work from so I will assume you’ve read over that and understood it (you if haven’t read it, do so now).

  • Organization and code

    In the case on StackOverflow, the logic was all in the observer, not the TracingListener. It also makes a good example of how to get selected data.

    
    
    var myObserver = {
       observe: function(aSubject, aTopic, aData){
              if (aTopic == 'http-on-examine-response')
              {
                    var oHttp = aSubject.QueryInterface(Ci.nsIHttpChannel);
                    var interfaceRequestor =   oHttp.notificationCallbacks
                                                   .QueryInterface(Ci.nsIInterfaceRequestor);
                    aSubject.DOMWindow = interfaceRequestor.getInterface(Ci.nsIDOMWindow);
              }
         }
       },
    
       QueryInterface: function(iid){
          if (!iid.equals(Ci.nsISupports) &&
              !iid.equals(Ci.nsIObserver))
            throw Components.results.NS_ERROR_NO_INTERFACE;
    
          return this;
       }
    }
    
    • How does it work?

      The three lines after our topic check very simply QueryInterface into nsIHttpChannel (like we had to with getting the URI and requestMethod), then the tricky bit is the next two steps: getting the channel’s notificationCallbacks and QueryInterface-ing into an nsIInterfaceRequestor, and then calling getInterface to get an nsIDOMWindow. nsIInterfaceRequestor provides a single method, getInterface, which is very similar to QueryInterface, but not the same (See the nsIInterfaceRequestor docs for more info).

  • Finishing up

    All that’s left is to register the observer

    
    var observerService = Cc["@mozilla.org/observer-service;1"]
        .getService(Ci.nsIObserverService);
    
    observerService.addObserver(myObserver,
        "http-on-examine-response", false);
    


Oct 11 2009

Howto: XHR Listening by a Firefox Addon

Category: Firefox, JavaScript, Pirate QuestingJonathan Fingland @ 4:00 am

The following post draws significantly from a post by Jan Odvarko at http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/ but goes a bit further. There are also some sections which were inspired by Firebug, but are heavily modified.

  • What you need to know

    Before I get into the code, understand that one of the most important things in this process to understand is that your extension’s listener is just one in a chain. It is the responsibility of every listener in the chain to pass on the information. Failure to do this has some amusing consequences…. like nothing loading in the browser.

    Just to make it really clear — Don’t drop the ball. (Edit: And while you can edit the data in the stream — don’t do it unless you have a really good reason.)

  • Convenience methods and aliases

    A lot of the Firefox internals are accessed using Components.classes and Components.interfaces. While the verbosity makes it clear, it can at times be overly repetitive and, honestly, can take a long time to write out. A fairly common shorthand is in use, Cc and Ci with a few other less common shorthands like CCIN (for creating instances of a class based on a class name and an interface name) and CCSV (similarly creating a service based on a class name and interface name)

    
    if (typeof Cc == "undefined") {
    	var Cc = Components.classes;
    }
    if (typeof Ci == "undefined") {
            var Ci = Components.interfaces;
    }
    if (typeof CCIN == "undefined") {
    	function CCIN(cName, ifaceName){
    		return Cc[cName].createInstance(Ci[ifaceName]);
    	}
    }
    if (typeof CCSV == "undefined") {
    	function CCSV(cName, ifaceName){
    		if (Cc[cName])
    			// if fbs fails to load, the error can be _CC[cName] has no properties
    			return Cc[cName].getService(Ci[ifaceName]);
    		else
    			dumpError("CCSV fails for cName:" + cName);
    	};
    }
    • What’s with all of the typeof checks?

      Firebug, gotta love it, but it declares the same things using const. Inside of an if() block, a const is still seen and conflicts, even when the if condition evaluates to false. The code above is essentially a workaround to satisfy both possibilities. If the user has firebug installed, then carry on; if the user doesn’t have firebug installed, declare those shorthands

  • The constructor

    function TracingListener() {
    }

    Above is a (very) simple constructor function for us to create objects from. The methods and properties on the prototype are below. Note that while I could have changed the structure to accommodate better data-hiding, the method below reduces the number of new functions created by making them all declared only once on the prototype. Functions in the constructor are recreated every time the constructor is called with new yourConstructor() whereas functions on the prototype are shared by all instances.

  • The prototype definition

    • Basic properties

      TracingListener.prototype =
      {
          originalListener: null,
          receivedData: null,   //will be an array for incoming data.
      

      The first part of the prototype definition is setting up some basic properties. Note that both are assigned null. These properties will exist on all instances of TracingListener, and thus not be undefined if/when checking. In the case of receivedData, do not be tempted to make it an array here. Remember that methods and properties on the prototype are shared by all instances of the same type — and we don’t want all instances to share the same array for data.

      Also worth note is that receivedData is a good candidate for data-hiding and declaring it local to the constructor… but scope and visibility limitations would mean the functions requiring access to it would either need to be in the constructor as well, or have accessor and mutator methods for it. If you’re making a Singleton or a small number of instances, declaring functions in the constructor is no big deal, but this listener will be instantiated hundreds or thousands of times and it’s important to keep the duplication to a minimum.

    • Methods on the prototype

      • Interface Requirements
            //For the listener this is step 1.
            onStartRequest: function(request, context) {
            	this.receivedData = []; //initialize the array
        
        	//Pass on the onStartRequest call to the next listener in the chain -- VERY IMPORTANT
        	this.originalListener.onStartRequest(request, context);
            },

        onStartRequest is the first thing called when the actual request processing begins. This is also the best opportunity to initialize the array on this listener.

            //This is step 2. This gets called every time additional data is available
            onDataAvailable: function(request, context, inputStream, offset, count)
            {
               var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1",
                                         "nsIBinaryInputStream");
                binaryInputStream.setInputStream(inputStream);
        
                var storageStream = CCIN("@mozilla.org/storagestream;1",
                                         "nsIStorageStream");
                //8192 is the segment size in bytes, count is the maximum size of the stream in bytes
                storageStream.init(8192, count, null); 
        
        	var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1",
                                         "nsIBinaryOutputStream");
                binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));
        
                // Copy received data as they come.
                var data = binaryInputStream.readBytes(count);
        
                this.receivedData.push(data);
        
                binaryOutputStream.writeBytes(data, count);
        
                //Pass it on down the chain
                this.originalListener.onDataAvailable(request,
                                                  context,
                                                  storageStream.newInputStream(0),
                                                  offset,
                                                  count);
            },

        onDataAvailable essentially copies the data from the binaryInputStream to our receivedData array and to the storageStream (via the binaryOutputStream). Then we pass a new InputStream from our storageStream onto the next listener in the chain.

            onStopRequest: function(request, context, statusCode)
            {
        	try
        	{
                        //QueryInterface into HttpChannel to access originalURI and requestMethod properties
        		request.QueryInterface(Ci.nsIHttpChannel);
        
                        //this is specific to the PirateQuesting Add-on, but is left here as an example of how to modify behaviour based on the requested URL
        		if (request.originalURI
                            && piratequesting.baseURL == request.originalURI.prePath
                            && request.originalURI.path.indexOf("/index.php?ajax=") == 0)
        		{
        
        			var data = null;
        			if (request.requestMethod.toLowerCase() == "post")
        			{
        				var postText = this.readPostTextFromRequest(request, context);
        				if (postText)
        					data = ((String)(postText)).parseQuery();
        
        			}
        
                                //Combine the response into a single string
        			var responseSource = this.receivedData.join('');
        
        			//fix leading spaces bug
        			//(FM occasionally adds spaces to the beginning of their ajax responses...
                                //which breaks the XML)
        			responseSource = responseSource.replace(/^\s+(\S[\s\S]+)/, "$1");
        
                                //gets the date from the response headers on the request.
                                //For PirateQuesting this was preferred over the date on the user's machine
        			var date = Date.parse(request.getResponseHeader("Date"));
        
                                //Again a PQ specific function call, but left as an example.
                                //This just passes a string URL, the text of the response,
                                //the date, and the data in the POST request (if applicable)
        			piratequesting.ProcessRawResponse(request.originalURI.spec,
                                                       responseSource,
                                                       date,
                                                       data);
        		}
        	}
        	catch (e)
        	{
        		//standard function to dump a formatted version of the error to console
        		dumpError(e);
        	}
        	//Pass it on down the chain
        	this.originalListener.onStopRequest(request,
                                                 context,
                                                 statusCode);
            },

        The onStopRequest above has a few tricky parts. The first is the QueryInterface to nsIHttpChannel – this is critical to getting the info needed. The second tricky part is to get the posted variables. To do so, you need to check that the requestMethod was indeed post, and then we call readPostTextFromRequest which I’ll introduce in a bit. The last tricky bit is getting the Date header from the response. Date.parse() plays nicely with those (assuming the server response conforms)

            QueryInterface: function (aIID) {
                if (aIID.equals(Ci.nsIStreamListener) ||
                    aIID.equals(Ci.nsISupports)) {
                    return this;
                }
                throw Components.results.NS_NOINTERFACE;
            },

        This is pretty standard for anything fulfilling an interface contract for Firefox (or other mozilla-based browsers). QueryInterface is part of the nsISupports interface and is the only part which is scriptable. All interfaces are derived from nsISupports, so it has to be there.

      • Utility methods

        The following methods are required by our TracingListener but are not part of the interface contract. (It would also have been possible to define them globally or within a pseudo-namespace.)

            readPostTextFromRequest : function(request, context) {
                try
                {
        	        var is = request.QueryInterface(Ci.nsIUploadChannel).uploadStream;
        	        if (is)
        	        {
        	            var ss = is.QueryInterface(Ci.nsISeekableStream);
        	            var prevOffset;
        	            if (ss)
        	            {
        	                prevOffset = ss.tell();
        	                ss.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
        	            }
        
        	            // Read data from the stream..
        		    var charset = "UTF-8";
        		    var text = this.readFromStream(is, charset, true);
        
        	            if (ss && prevOffset == 0)
        	                ss.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
        
        	            return text;
        	        }
        		else {
        			dump("Failed to Query Interface for upload stream.\n");
        		}
        	    }
        	    catch(exc)
        	    {
        			dumpError(exc);
        	    }
        
        	    return null;
        	},

        I will readily admit that readPostTextFromRequest is mostly taken from Firebug, though there are a few changes. Basically, we have to do the same thing as before and QueryInterface into the appropriate interface. In this case we need nsIUploadChannel to get access to uploadStream. And then we QueryInterface the uploadStream into a nsISeekableStream (noticing a pattern, yet? QueryInterface is your best friend.. and worst enemy.). After that we store the original offset in the stream in prevOffset, and then seek to the beginning of the stream. Then we read the data and, if the stream was at position 0 originally, we seek to the beginning again.

        	readFromStream : function(stream, charset, noClose)	{
        
        	    var sis = CCSV("@mozilla.org/binaryinputstream;1",
                                    "nsIBinaryInputStream");
        	    sis.setInputStream(stream);
        
        	    var segments = [];
        	    for (var count = stream.available(); count; count = stream.available())
        	        segments.push(sis.readBytes(count));
        
        	    if (!noClose)
        	        sis.close();
        
        	    var text = segments.join("");
        	    return text;
        	}
        
        }

        readFromStream is also largely from Firebug with a few modifications. It is however remarkably similar to what is done in onDataAvailable and onStopRequest. Basically, we get a BinaryInputStream to work with the stream given. Then we loop through the segments of the stream (size provided by available()) and add them to an array. When finished with that, we join the segments and return the text.

        httpRequestObserver = {
        
        	observe: function(request, aTopic, aData){
        		if (typeof Cc == "undefined") {
        			var Cc = Components.classes;
        		}
        		if (typeof Ci == "undefined") {
        			var Ci = Components.interfaces;
        		}
        	    	if (aTopic == "http-on-examine-response") {
        	    		request.QueryInterface(Ci.nsIHttpChannel);
        
        			if (request.originalURI
                                    && piratequesting.baseURL == request.originalURI.prePath
                                    && request.originalURI.path.indexOf("/index.php?ajax=") == 0) {
        				var newListener = new TracingListener();
            				request.QueryInterface(Ci.nsITraceableChannel);
            				newListener.originalListener = request.setNewListener(newListener);
        			}
        		}
        	},
        
        	QueryInterface: function(aIID){
        		if (typeof Cc == "undefined") {
        			var Cc = Components.classes;
        		}
        		if (typeof Ci == "undefined") {
        			var Ci = Components.interfaces;
        		}
        		if (aIID.equals(Ci.nsIObserver) ||
        		aIID.equals(Ci.nsISupports)) {
        			return this;
        		}
        
        		throw Components.results.NS_NOINTERFACE;
        
        	},
        };

        This part is fairly straightforward. The object httpRequestObserver has to fulfill the contract for the nsIObserver interface — which only has two methods: observe and QueryInterface.

  • Observer registration

    Finally, we need to register the observer:

    var observerService = Cc["@mozilla.org/observer-service;1"]
        .getService(Ci.nsIObserverService);
    
    observerService.addObserver(httpRequestObserver,
        "http-on-examine-response", false);

    Now the observerService will call the observe method on httpRequestObserver whenever it notifies observers with the http-on-examine-response topic.

    When you want to unregister the observer, use:

    observerService.removeObserver(httpRequestObserver,
        "http-on-examine-response");

    As you can see, getting the text and post variables from an http request is non-trivial.

Note, though, that this code does not check the context to determine whether the http request is for a browser window, or from a browser window so depending on the complexity of your situation, you may want to do that as well. Perhaps, I’ll add that in another post.

(See Firebug license here. Special thanks to the Firebug team and to Jon Odvarko for providing so much useful material. The interface docs at oxymoronical are a great resource. The Mozilla Developer Center also deserves special credit for great documentation. )

Update (Jan 17, 2010): Corrected a small bug in onStopRequest (Thanks Broady!). See below for details.

Update (April 22, 2010): Corrected a bug which doesn’t occur if Firebug is installed (Thanks Harini!). See below for details.

Tags: , , ,


Oct 09 2009

Implementing an XPCOM Firefox Interface and Creating Observers

Category: Firefox, JavaScriptJonathan Fingland @ 4:05 am

There are lots of cases when it is desirable to implement one of the XPCOM interfaces in use by Firefox, or other mozilla-based browsers. There are three cases where PirateQuesting does so, but once you see the concept, it should be easy to adapt to your situation.

  • You must have a QueryInterface to enjoy this ride

    First off, all XPCOM interfaces in Firefox inherit from nsISupports (Also see details on oxymoronical.com here). Only one method is scriptable and part of XPCOM — QueryInterface — and it must be present in all implementations of XPCOM interfaces.

    
    //"implements" nsISupports
    var InterfaceImplementation = function() {
      QueryInterface: function (aIID) {
          if (aIID.equals(Components.interfaces.nsISupports))
          {
             return this;
          }
          throw Components.results.NS_NOINTERFACE;
      }
    }
    

    The above is an example of the very minimum required to support any interface. QueryInterface requires a first parameter which is an aIID from Components.interfaces.*. There is also a second, optional, parameter, but as I have never come across this in use, it’s not worth pursuing here.

  • Now what?

    A very common (and useful) use of XPCOM interface implementation is creating your own observers, for example:

    
    var myObserver = {
    
      observe: function(request, aTopic, aData){
        if (aTopic == "http-on-examine-response")
        {
          //response has come back, now what?
        }
        else if (aTopic == "http-on-modify-request")
        {
          //opportunity to modify headers on request
        }
      },
    
      QueryInterface: function(aIID){
         if (aIID.equals(Components.interfaces.nsIObserver) ||
             aIID.equals(Components.interfaces.nsISupports))
        {
          return this;
        }
        throw Components.results.NS_NOINTERFACE;
      },
    };
    

    The nsIObserver interface is fairly simple as it only adds one new method. As you can see now, though, QueryInterface now checks for both nsIObserver and nsISupports. Remember: any interface you implement must have a QueryInterface supporting all interfaces in the inheritance chain.

  • Observer registration

    If you then wanted to register your observer, it’s as easy as:

    
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                   .getService(Components.interfaces.nsIObserverService);
    
    observerService.addObserver(myObserver,"http-on-examine-response", false);
    observerService.addObserver(myObserver,"http-on-modify-request", false);
    

    Then to unregister, you do:

    
    observerService.removeObserver(myObserver, "http-on-examine-response");
    observerService.removeObserver(myObserver, "http-on-modify-request");
    

    When I have a chance, I’ll add a more complete example of using observers to watch http requests, but in the meantime check out the list of Observer Notifications at MDC.

Tags: , ,


Sep 24 2009

Singletons in JavaScript

Category: JavaScript, PHPJonathan Fingland @ 4:13 pm

I use singletons all over the PirateQuesting code, but I’ve never explained the idea here, nor how they are different from singletons in other languages.

Anyone familiar with common design patterns knows how singletons work in the vast majority of languages. For a PHP example, see this post.

In JavaScript there are basically two main ways to create a singleton. One is use immediate instantiation so that the class may not be instantiated again. The second is closer to the traditional singleton pattern, however, since there are no private members or constructors, we can get around this by using a nested ‘class’ constructor — and there are two ways to accomplish this.

The first way is done as follows:

var singleton = new (function Singleton() {
                               this.value = 12;
                             } )();

alert(singleton.value); // alerts 12;

var singly = new Singleton(); // Singleton is not defined.

alert(singleton instanceof Singleton); // Singleton is not defined

In the above, the constructor name, Singleton, isn’t really necessary. While it can’t be used to instantiate the Singleton again, it also can’t be used with instanceof or typeof so feel free to leave it out if you want; leaving it in, does make it more obvious what is happening though.

And the second way (version 1):

var Singleton = (function () {
  var instance = null;

  function Singleton() {
    this.value = 12;
  }

  this.getInstance = function () {
    if (instance === null) {
      instance = new Singleton();
    }
    return instance;
  }
})();

var singleton = Singleton.getInstance();
alert(singleton.value); // alerts 12;

var singly = new Singleton(); // Singleton is not a construct

alert(singleton instanceof Singleton); //invalid 'instanceof' operand Singleton

The above is just an anonymous function being called immediately (note the () after the function declaration). Inside the function it declares a pseudo-private member called instance, a pseudo-private constructor, Singleton, and a getInstance method. The getInstance method can see instance due to closure, while the outside world has no idea that it exists.

And finally, the second way (version 2):

var Singleton = {

  getInstance : (function() {

    var instance;
    function Singleton() {
      this.value = 12;
    }

    return function () {
      if (instance == null) {
	    instance = new Singleton();
      }
      return instance;

    }

  })()
}

var singleton = Singleton.getInstance();
alert(singleton.value);

var singly = new Singleton(); // Singleton is not a constructor

alert(singleton instanceof Singleton); //invalid 'instanceof' operand Singleton

In this final example, all of the logic is put into the getInstance function which is the result of a self-calling function which itself returns a function. Yeah, it’s a little hard to follow, but basically the returned function has visibility of instance and the Singleton constructor due to closure.

As a closing note, it’s worth pointing out that although all of the above methods do not permit a second instance construction, they also do not allow use of typeof or instanceof for type verification.

Update

Based on a comment below by Peter Robinett, I’ve come up with a different way to do this, which allows for instanceof and typeof and seems to be an all-round good solution to the problem. It does not, however use the classical getInstance method, but works well all the same.

var Singleton = function () {
  var instantiated = false;

  return function () {
    if (instantiated) {
      throw "Singleton already instantiated";
    }
    instantiated = true;
    this.value=12;
    }
}()

var singleton = new Singleton();
alert(singleton.value);    //alerts 12
alert(singleton instanceof Singleton);  alerts true 

var singly = new Singelton(); //throws exception and cannot be instantiated

Tags: , ,


Apr 20 2009

Prototype’s Bind and Curry

Category: JavaScriptJonathan Fingland @ 12:58 pm

I’ve been a fan of both Prototype and JQuery for a while and have no qualms recommending either. They do have somewhat different aims, and as such it’s a good idea to consider carefully which one best meets the needs of your project (or if you even need either to meet your needs). I’ll leave that discussion for another post, though.

Today, I’m gonna show you two very useful functions in prototype. Show you how to use them, and show you how they work.

First off, curry with a quick example:

function show_name(name)
{
    alert(name);
}

var show_my_name = show_name.curry("Jon");
show_my_name();

The call to show_my_name will result in an alert dialog with “Jon”. The curry function allowed me to pre-load the parameters. You can also do a partial curry as follows:

function show_animal(animal_type,adjective)
{
    alert("It is a " + adjective + " " + animal_type);
}

var show_dog = show_animal.curry("dog");

show_dog("fast");
show_dog("brown");

The above will result in two alert dialogs — the first saying “It is a fast dog” and the second saying “It is a brown dog”. the show_dog method only required the one parameter, because the animal type had already been pre-loaded.

Bind works in an almost identical way but with one important difference — context switching. The first parameter to bind is the desired context. For example:

var fancy_box = function(element)
{
     //ensure element is a checkbox. if not, bail
     if ((!(element instanceof HTMLElement)
         || !(element.nodeName.toLowerCase() === "input")
         || !(element.getAttribute("type").toLowerCase()  === "checkbox"))
    {
         return;
    }

    this.isChecked = function()
    {
        return !!element.checked;
    }

    this.click_handler = function()
    {
        var message = this.isChecked() ? "Checked" : "Not Checked";
        alert(message);
    }

    element.observe("click", this.click_handler.bind(this)); //observe is a prototype stand-in for addEventListener/attachEvent to allow you to ignore browser differences.
}

The element.observe line above makes use of bind to ensure the click_handler is working in the correct context. You can, however, use a different context instead.

Event handlers can be tricky and there is actually a specialized version of bind, called bindAsEventListener which works the same as bind, but also passes the event.


So…. How do they work? Well, both curry and bind (okay, and bindAsEventListener, too) rely on built in JavaScript methods call and apply.

call and apply are very similar and really only differ in one significant way. Both take the operating context as the first parameter but apply takes the arguments as an array whereas call takes the arguments as regular parameters.

To use an earlier example:

show_name.call(window,"Bob");

This will result in show_name operating in the window context and being given the parameter "Bob". This can be especially important when you use these methods on objects which don't natively support them, though.

Every function has a special variable called arguments which is like an array but isn't. It's array like behavior, though, makes the Array functions usable on it as in the following:

function say()
{
var message = Array.prototype.join.call(arguments," ");
alert(message);
}
say("Hello","Bob");

Even though the function say() doesn't explicitly take any arguments, what it really does is take a variable number of arguments, and joins them together, separated by spaces, and then alerts them. This kind of technique can be quite handy in lots of situations, but in this post, I'm concerned about how it will help us understand bind and curry.

From Prototype 1.6.1:

Object.extend(Function.prototype, (function() {
  var slice = Array.prototype.slice;

  function update(array, args) {
    var arrayLength = array.length, length = args.length;
    while (length--) array[arrayLength + length] = args[length];
    return array;
  }

  function merge(array, args) {
    array = slice.call(array, 0);
    return update(array, args);
  }

  function bind(context) {
    if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
    var __method = this, args = slice.call(arguments, 1);
    return function() {
      var a = merge(args, arguments);
      return __method.apply(context, a);
    }
  }

  function bindAsEventListener(context) {
    var __method = this, args = slice.call(arguments, 1);
    return function(event) {
      var a = update([event || window.event], args);
      return __method.apply(context, a);
    }
  }

  function curry() {
    if (!arguments.length) return this;
    var __method = this, args = slice.call(arguments, 0);
    return function() {
      var a = merge(args, arguments);
      return __method.apply(this, a);
    }
  }

//...trimmed
}

First lets look at curry again.

    if (!arguments.length) return this;

This just means we should bail if there are no arguments and simply return the original function.

    var __method = this, args = slice.call(arguments, 0);

Two things are being done here. First, __method is storing a reference to this for later use. This allows us to make use of a closure in the following anonymous function. The second thing is a call using Array.prototype.slice (though shortened by prototype for reuse) which allows us to convert the arguments object into an array. Handy.

    return function() {

Here we return an anonymous function. This is how function.curry(parameter) returns a function we can use later, for example, in an event handler or other callback situation.

      var a = merge(args, arguments);

This is a fairly straightforward to read. The variable a contains a merged array of args and arguments. args is available via a technique known as closure. args, declared in the wrapping function is visible to, or within scope of, the anonymous function being returned. If you're unsure what the functions merge and update do, just know that merge simply converts the first parameter to an array and calls update. update then loops through args and appends each element to arguments (array in the update function).

      return __method.apply(this, a);

Finally, our anonymous function returns the value from the original method, with the combined arguments of the original call to curry, and the arguments passed to the anonymous function when it was called.

Whew.

Still with me?

Okay, lets go.

Bind is a little more complicated, but not much, and if you understand curry, this shouldn't be much of a problem.

  function bind(context) {
    if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;

First important difference: bind requires a context parameter. If the first argument is undefined and there is fewer than two parameters, then there really isn't anything to be done, and bind simply returns the original function.

    var __method = this, args = slice.call(arguments, 1);

This is virtually identical to curry, except that instead of converting the entire arguments array, we only want the second element onwards.

    return function() {
      var a = merge(args, arguments);

Same as curry.

      return __method.apply(context, a);

Slightly different from curry again. Instead of this, bind uses apply with a context.

For more examples on Prototype extensions to the Function prototype, see http://api.prototypejs.org/language/function.html


For those familiar with closures, you may think this is totally unnecessary -- and you may be right -- if you only need to make use of this once or twice in your web app, but if you frequently find yourself needing closures just to refer to this then maybe it's worth generalizing into a routine, or going with a framework that has it built in.


Apr 02 2009

JavaScript’s default operator

Category: JavaScriptJonathan Fingland @ 11:30 am

Here’s something pretty easy to use, useful, and strangely not as common as you would expect.

JavaScript has an operator known as the “default” operator and is the same as the logical-or operator. That’s right. ||.

First, some examples of how we can use it:

var value_1 = false || 2;
var value_2 = null || 2;
var value_3 = 0 || 2;

In all three of the above cases, the left side of the default operator is falsey and thus value_1, value_2, and value_3 are all equal to 2. Just to make it clear, falsey values are values which can be converted to boolean false. ‘false’ itself is a given, null is considered false, so are empty strings and finally and so is 0. Negative numbers are not false and nor are objects, empty or otherwise. Thus:

Truthy values Falsey Values
  • Non-zero number
  • Object
  • Boolean true
  • Non-empty string
  • Zero (0)
  • Null
  • Boolean false
  • Empty string

Below is a slightly more useful example. I’ve used jQuery’s $.get here to simplify the example and the display_items method is left to the imagination of the reader.

var DEFAULT_ITEM_LIMIT = 20; //In the case of a firefox extension, this would be better written as: const DEFAULT_ITEM_LIMIT = 20;

function get_random_items(how_many)
{
     how_many = how_many || DEFAULT_ITEM_LIMIT;
     $.get("random_items.php", {limit:how_many}, display_items);
}

In the above example, we see a reasonable default being set in advance and if the get_random_items method is called without passing parameters, or provided null, false, 0 or an empty string as the value of how_many, then the default item limit will be used instead.

It works pretty simply. If the statement on the left side is falsey, then the value on the right side is used instead — exactly like the logical-or. See this MDC page on logical operators for more examples.

Now you can do some fancy things with this. For example, you could do something like this:

var user_name;
if ((user_name = (prompt("What is your name?") || "Jon")) === "Jon")
{
    //do stuff
}

But please, please don’t. That line was painful to write, and unnecessarily difficult to read. Something like:

var user_name = prompt("What is your name?");
if (!user_name)
{
    //no name provided, use default
    user_name = "Jon";
}

… would be much better. Just remember, like most coding tricks, only use the default operator when it makes things more readable.

One of the most common, and useful examples of this technique comes with event handlers.

function box_click_handler(event)
{
    event = event || window.event;
    var target = event.target || event.srcElement;
    //do other stuff
}

Basically, by using the default operator we were able to quickly and easily get the event object and the target (or source) of the event in both cases: browsers that have strong DOM spec support, and browsers that don’t, i.e. IE.

As a closing note, just like the logical-or in a conditional, this too can be chained. The leftmost truthy value will be used.

function find_user(name)
{
    name = name || false || get_default_name();
    $.post("find_user.php", {name:name}, display_user);
}

Of course the above is needlessly long (the false is just thrown in for demonstration purposes), but I think you get the idea. Hope ya’ll find it useful.


Mar 25 2009

The loadOverlay Dilemma

Category: Firefox, JavaScript, Pirate Questing, XULJonathan Fingland @ 4:51 am

When making Firefox extensions with modular components, it’s nice to be able to include overlays based on preferences or some other criteria at loadtime. One of the problems that quickly comes up is that sequential loadOverlay calls will fail. This bug is documented here. The solution described there, and elsewhere, is to use chained observers and a custom queue. Strangely, I never came across an implementation example of such an observer/queue combination. So… with that stunning introduction, I give you the system used in PirateQuesting 2.

The first part here is an overlay registry. Essentially, the queue portion of the solution.

piratequesting.overlayRegistry = function() {
	var overlays = [];
	var index = 0;

	function conflicts(tabid, tabpanelid, overlayFile) {
		for (var i = 0, len = overlays.length; i<len;++i)
			if ((tabid == overlays[i].getTabId() && tabid != null )  || (tabpanelid == overlays[i].getTabPanelId() && tabpanelid != null) || overlayFile == overlays[i].getOverlayFile())
				return true;
		return false;
	}

	function overlay(tabid, tabpanelid, overlayFile) {
		var tabid = tabid;
		var tabpanelid = tabpanelid;
		var overlayFile = overlayFile;
		var added = false;

		return {
			toString : function() {
				return "tabid: " + tabid + "\ntabpanelid: " + tabpanelid
						+ "\noverlayFile: " + overlayFile;
			},
			getTabId : function() {
				return tabid;
			},
			getTabPanelId : function() {
				return tabpanelid;
			},
			getOverlayFile : function() {
				return overlayFile;
			},
			getAdded : function() {
				return added;
			},
			setAdded : function(val) {
				added = !!val; // ensure boolean
			}
		}

	}

	return {
		addOverlay : function(tabid, tabpanelid, stringFile) {
			if (!conflicts(tabid, tabpanelid, stringFile))
				overlays.push(new overlay(tabid, tabpanelid, stringFile));
			else
				dump("\nOverlay conflict occurred on: " + tabid + ", " + tabpanelid + ", " + stringFile);
		},
		getOverlayByIndex : function(index) {
			return overlays[index];
		},
		getOverlayByTabId : function(tabid) {
			var ol = overlays.length;
			for (var i = 0; i < ol; i++) {
				if (overlays[i].getTabId() == tabid)
					return overlays[i];
			}
			return false;
		},
		count : function() {
			return overlays.length;
		},
		reset : function() {
			index = 0;
		},
		next : function() {
			if (index < overlays.length) {
				return overlays[index++];
			} else
				return null;
		},
		progress : function() {
			return Math.ceil(index * 100 / overlays.length);
		},
		resetAll : function() {
			this.reset();
			var nex;
			while (nex = this.next()) {
				nex.setAdded(false);
			}
			this.reset();
		}

	}
}();

Now, that is a somewhat specialized case. The tabid and tabpanelid are arguably unnecessary but have been included to prevent two modules having the same tab ids, and, more importantly, to be able to refer to the overlay by a known value, in this case the tabid.

A somewhat stripped down version might look like:

var overlayRegistry = function() {
	var overlays = [];
	var index = 0;

	function conflicts(overlayFile) {
		for (var i = 0, len = overlays.length; i<len;++i)
			if (overlayFile == overlays[i].getOverlayFile())
				return true;
		return false;
	}

	function overlay(overlayFile) {
		var overlayFile = overlayFile;
		var added = false;

		return {
			toString : function() {
				return "\noverlayFile: " + overlayFile;
			},
			getOverlayFile : function() {
				return overlayFile;
			},
			getAdded : function() {
				return added;
			},
			setAdded : function(val) {
				added = !!val; // ensure boolean
			}
		}

	}

	return {
		addOverlay : function(stringFile) {
			if (!conflicts(stringFile))
				overlays.push(new overlay(stringFile));
			else
				dump("\nOverlay conflict occurred on: " + stringFile);
		},
		getOverlayByIndex : function(index) {
			return overlays[index];
		},
		count : function() {
			return overlays.length;
		},
		reset : function() {
			index = 0;
		},
		next : function() {
			if (index < overlays.length) {
				return overlays[index++];
			} else
				return null;
		},
		progress : function() {
			return Math.ceil(index * 100 / overlays.length);
		},
		resetAll : function() {
			this.reset();
			var nex;
			while (nex = this.next()) {
				nex.setAdded(false);
			}
			this.reset();
		}

	}
}();

The overlay registry is simply an iterator-style queue. This makes walking through the items very easy for the observer (shown next) which doesn’t (and shouldn’t) really have any idea of the state of the queue. The queue, overlayRegistry, makes a number of methods available for getting basic info about the queue (size, progress, etc) for use in progress bars or the like. It also provides ways of resetting the queue. Obviously, since it’s designed to be an iterator, there are ways of getting the next item and advancing the queue.

The second part of the solution is using a chained observer. Again, the piratequesting implementation is:

function overlayObserver()
{
  this.register();
}
overlayObserver.prototype = {
  observe: function(subject, topic, data) {

  	function cleanUp() {
		sidebar.contentDocument.getElementById("pqmain_deck").selectedIndex="1";

		var mod_boxes = sidebar.contentDocument.getElementsByTagName("tabbox");
		for (var i=0,len=mod_boxes.length;i<len;++i) {
			if (hasClassName(mod_boxes[i],"moduleBox")) {
				mod_boxes[i].selectedIndex = 0;
			}
		}
  	}

  	if (topic == "xul-overlay-merged") {
		try {
			var nex = piratequesting.overlayRegistry.next();
			if (nex) {
  				sidebar.contentDocument.getElementById("pqloadprogress").value = piratequesting.overlayRegistry.progress();
  				try {
  					sidebar.contentDocument.loadOverlay(nex.getOverlayFile(),this);
	  			} catch (error) {
  					cleanUp();
  					dump("Failed to load: " + nex.getOverlayFile() + "\nReported Error " + getErrorString(error));
	  			}
  			} else {
  				cleanUp();
	  		}
  		} catch (error) { alert(getErrorString(error)); }
  	}
  },
  register: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                          .getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(this, "xul-overlay-merged", false);
  },
  unregister: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                            .getService(Components.interfaces.nsIObserverService);
    observerService.removeObserver(this, "xul-overlay-merged");
  }
}

And a stripped down version would look something like this:

function overlayObserver()
{
  this.register();
}
overlayObserver.prototype = {
  observe: function(subject, topic, data) {

  	if (topic == "xul-overlay-merged") {
		try {
			var nex = overlayRegistry.next();
			if (nex) {
  				try {
  					document.loadOverlay(nex.getOverlayFile(),this);
	  			} catch (error) {
  					dump("Failed to load: " + nex.getOverlayFile() + "\nReported Error " + getErrorString(error));
	  			}
  			} else {
  				cleanUp();
	  		}
  		} catch (error) { alert(getErrorString(error)); }
  	}
  },
  register: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                          .getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(this, "xul-overlay-merged", false);
  },
  unregister: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                            .getService(Components.interfaces.nsIObserverService);
    observerService.removeObserver(this, "xul-overlay-merged");
  }
}

This observer chain is started with:

overlayObserver.observe(null,"xul-overlay-merged", null);

The observer code is also fairly simple but relies on some things that were not terribly well documented. The key thing to know about loadOverlay is that it raises an xul-overlay-merged observer notification. here, but you’ll notice what’s missing. This entry, however, has that piece of info. If you’ve never used observers before, this gives a good rundown.

Sooo…. What happens? how does it work? Basically, at some point during the initialization of the sidebar (not important in this discussion), the observer chain is started and will loop as follows:

  1. receive notification of the last loadOverlay finishing
  2. get the next item in the iterator
  3. if the “next item” is null, stop
  4. load the overlay file specified by said item

And you’re done. One Note I would make is that if there are multiple extensions making use of this at the same time, you’re very likely to have problems when both extensions receive the notifications and both start loading their next items. As firefox still lacks a built-in queue for overlay loading, we’re stuck hoping that nobody else will use this at the same time.

Note: You may notice that a lot of the piratequesting code makes use of a function getErrorString(). This is a very siple function that puts all of the error info I want into a string. dump() is a function available in firefox for dumping text to the console. I am also currently working on an error logging system and will cover all of these issues in more detail when that is finished. For the time being, ignore how I handle the errors but for obvious reasons, you will want to have some kind of error handling in place.

Edit 2009/04/04:
It has since occurred to me that by using additional information, that is, subject, from the observer notification, I can reduce the chances of conflict by ensuring it only fires on the completion of it’s own loadOverlay calls.


Dec 01 2008

Functions in Closures

Category: DOM, JavaScriptJonathan Fingland @ 7:47 am

I last wrote about closures and how this affects the visibility of variables and functions. This time I’m going to introduce returning functions from a closure and how it can be useful in the first part.

First off, lets say you have a strange need to create functions to consistently add a value like addFive, addTen, etc. Well this is one way to go about it.

function createAdder(addition_size) {
      return function (value) {
                     return value + addition_size;
               }
}

var addFive = createAdder(5);
var addTen = createAdder(10);

alert(addFive(3));
alert(addTen(-10));

The code above will alert 8 and 0. While this isn’t the most natural use of the technique, hopefully it is clear enough. addition_size is retained by the addFive and addTen functions because the function had visibility when it was declared.

One handy way to use this is when creating event handlers that need to take parameters. When adding a handler, you pass the function itself, which doesn’t provide an opportunity to add extra parameters, we can get around this by doing:

function getValuePlusFiveDisplayer(value) {
  return function (event) {
    var sum = addFive(value);
    alert(value + " + 5 = " + sum);
  }
}

var number_1 = document.getElementById("number_1");
number_1.addEventListener("click",getValuePlusFiveDisplayer(1),false);

var number_2 = document.getElementById("number_2");
number_2.addEventListener("click",getValuePlusFiveDisplayer(2),false);

And HTML like:

<span id="number_1" class="number_button">1</span>
<span id="number_2" class="number_button">2</span>

Which would work like this:

12

Even though you’re unlikely to need this exact example, hopefully it gives you some ideas, or helps solve some problems

Tags: ,


Nov 28 2008

JavaScript Closures

Category: JavaScriptJonathan Fingland @ 1:26 am

Closures are one of the most powerful features of JavaScript. JavaScript isn’t the only language supporting this, but it’s certainly the one I’m going to discuss here. Just for example, the same thing can be done in ActionScript 3 — if you know how that works, you know how it works in JavaScript.

Getting down to the matter at hand, closures are essentially a way of managing variable scope. First lets look at some scope basics, though.

function User(name, age, address) {
     //name, age and address are all private to the scope of this function

     this.name = name;    // public property

     this.getAge = function () { return age; }; //public, privileged method

     function getAddress() { return address; }  //private method

}

User.prototype.getName = function () { return this.name; } //public, non-priveleged method

User.prototype.getAddress = function() { return address; } //fails. address is undefined in this scope.

Just to make it clear, you can have public properties and private variables, public — privileged and non-privileged — methods, and private methods. Privileged methods can access the variables and methods in the constructor (as well as the public ones), whereas non-priveleged methods can only access public members and methods.

Last week, I wrote this:

var myButton = document.getElementById("myButton");
myButton.addEventListener("click",
                               function (event) {
                                     alert("you clicked!");
                               },
                               false);

Now lets combine the two ideas:


function User(element, name, age, address) {
     //name, age and address are all private to the scope of this function

     this.name = name;    // public property

     this.getAge = function () { return age; }; //public, privileged method

     function getAddress() { return address; }  //private method

     element.addEventListener("click", function (event) { alert(this.name + "\n" + age + "\n" + getAddress()); }, false);

}
var myButton = document.getElementById("myButton");
var jon = new User(myButton, "Jon", 98, "123 fourth street, yourtown");

Now, if I click on myButton I’ll get an alert with all of the information…. except name. When the event handler executes, the value of this has changed. The easiest way to get around this is by taking advantage of closures and not use this by using var self = this;


function User(element, name, age, address) {
     //name, age and address are all private to the scope of this function

     this.name = name;    // public property

     this.getAge = function () { return age; }; //public, privileged method

     function getAddress() { return address; }  //private method
     var self = this;
     element.addEventListener("click", function (event) { alert(self.name + "\n" + age + "\n" + getAddress()); }, false);

}
var myButton = document.getElementById("myButton");
var jon = new User(myButton, "Jon", 98, "123 fourth street, yourtown");

Now it works. it’s a very small change and it means that by taking advantage of closures we can maintain a reference to this even when the context changes. This technique is extremely common in event handlers, but it’s also very useful when using setTimeout or setInterval (which use window as context)

Another use of this is to wrap functions that can see internal data. I’ll explain that more in the next post.


Nov 14 2008

Extending Prototypes

Category: JavaScriptJonathan Fingland @ 10:14 am

JavaScript is described as a prototypal language because of it’s use of a shared prototype/cookie cutter for objects. It’s probably easier to understand with some examples, though.

function User (name) {
    this.name = name;
}

var jon = new User("Jon");
alert(jon.name);

The above will show an alert dialog with the name of the user, “Jon”. All instances of User will have a name property, but we can can do more:

function User (name) {
    this.name = name;
    this.say_name = function() {
        alert(this.name);
    }
}

var jon = new User("Jon");
jon.say_name();

Now all users will have a method, say_name. Unfortunately, every User instance has a new method called say_name. It's not a complex function, but there's potentially a lot of needless duplication. If say_name doesn't need to know private details, we can do the following instead:

function User (name) {
    this.name = name;
}

User.prototype.say_name = function() {
    alert(this.name);
}

var jon = new User("Jon");
jon.say_name();

Now all User instances share the same say_name function.

A couple of days ago, I posted about First-class functions in JavaScript and in particular the map function. As written, it works fine, but how could we take advantage of the prototype property shown above. I mentioned that JavaScript 1.6 adds support for a map function, but older browsers don't support it so we can do this instead:

if (typeof Array.prototype.map == "undefined") //check if it isn't already defined (e.g. IE 6)
{
    Array.prototype.map = function (callback) {
        var ret_array = [];
        for(var index = 0, length = this.length; index < length; ++index) {
            ret_array[index] = callback(this[index]);
        }
        return ret_array;
    }
}

Now there is one small problem. The map function defined in JavaScript 1.6 takes an optional 2nd parameter for the context of this. We can fix the above by changing the method as follows:

    Array.prototype.map = function (callback, context) {
        var ret_array = [];
        for(var index = 0, length = this.length; index < length; ++index) {
            ret_array[index] = callback.call(context, this[index]);
        }
        return ret_array;
    }

I've used the call method which belongs to all Function objects. See call at MDC for more detail on that. It allows you to change the context of the execution of a function... letting 'this' refer to something different. I'll write more about that later as it really deserves its own post.

So, now we have a standard compliant, backwards-compatible (back to JavaScript 1.3 compliant browsers) map function. If the browser supports it, the native map function will be used, and if not, our substitute will be used instead. Feel the Win.

There's a lot more you can do with this, but that should be enough to get started.


Next Page »