Snippets code from my daily experience

December 28, 2008

Spread XUL (Sunday Thought)

Filed under: sunday_thought,Uncategorized,xul — dafi @ 11:26 am

My friend Hamen made me a question about context menu on statusbarpanels, the answer was very trivial for me but I realized should be complicated to find XUL snippets using only web search engines (eg googling).

Are duplicated informations present on the web the hell?

Suppose you need to write some code to read a socket in C/C++ or Java or your preferred language and you use Google or Yahoo to find a snippet, your search produces millions of¬† interesting results so the information is duplicated and this duplication can confuse you but it has a great advantage, you can copy and paste like a monkey ūüėõ discovering the code fits your needs.

Please consider duplication different from clone, duplication from my point of view means “the same topic explained many times in different manner”, clone means “exactly the same code reported in many different places”.

I don’t consider duplicated informations the hell because you can compare found results, you can discover new techniques or simply you can study deeply some aspect of the original problem.

Today XUL/XPCOM searches rarely produces hundreds (certain not millions) of results, the developer is like a pioneer.

I like to be a pioneer ūüėČ but a novice developer can feel discouraged.

XUL developer isn’t alone, he/she has many friends

Ok, actually web search results about XUL are not comparable to other programming languages but developer can use al least two types of resources: human and software

  • Human

The irc channels are great, I always found people ready to help me, also forums and mailing lists are very useful.
Honestly I consider some forum less useful, rarely I’ve received reply to my posts but I’m sure my ugly english didn’t helped me.

  • Software

lxr is the first resource developer must learn to use, searching XUL code directly inside code

MDC and XUL Planet don’t require presentation ūüėČ

Many novice developers don’t know DOMInspector, this is also due to the fact last Firefox releases removed it from standard installation

Help the community to help you

I admit, IRC is my preferred way to ask help but to speed up the process I try to prepare the code to discuss about so developer encounter another friend, the pastebin service.

I’ve written a Komodo macro that quickly submits a code selection to pastebin detecting language and other stuff.

You can find more info here, I’m sure other text editors allow similar solutions¬† (if they don’t, drop them and use Komodo)

Unzip the world

Dear novice XUL developer, you must simply unzip the world!

What you are trying to do probably is already yet been done, so unzip extensions and take a look at their javascript/xul code.

The 80% of extensions don’t contain platform specific binary code so you can find all inside the XPI.

If you can’t access to Internet try to obtain a tarball with Firefox (or any other Mozilla application) source code and use your editor ‘Find in Files’ feature.

Take a look also inside the chrome directory of your installed Firefox ūüėČ

I will try to spread XUL

Using this blog I want to create duplicated informations ūüėÄ (garbage??)

I will start a new section dedicated to XUL snippets for beginners users (like me at all), this snippets will be ready to run (when possibile).

I hope to spread XUL know-how starting from the basics.


December 24, 2008

Dear Santa Claus…

Filed under: Uncategorized — dafi @ 2:22 pm

… I would like

  • to get a Mac Book Pro
  • to be invited to participate to a Mozilla event
  • to be hired for an amazing job possibly where I can use my XUL know-how
  • to mate a wonderful blond girl (like this) ūüėõ
  • to watch the last Heroes and Lost series

December 22, 2008

Downloading protected resources using nsIChannel and friends

Filed under: babelzilla,extension,komodo,macro,nsIChannel,nsIStreamListener — dafi @ 2:35 pm

A couple of days ago I needed to automate file downloading from a service, a very trivial task in every programming language (or using wget).

The little complication was represented by the web based authentication mechanism (userid/password) needed to access to files.
Determining which files to download and their usage (unzipping and picking files) required some specific business logic, nothing really complicated but very annoying.

After a while I realized this job can be done using Javascript and XPCOM and here I would share the solution based on nsIChannel.

What is does

  • the service uses userid and password to login
  • login to the service using HTTP POST method, it simulates an HTML <form/> submission
  • store the cookies sent from server. They contains credentials login data, cookies usage is a prerequisite in our scenario
  • reuse login data to download protected resources

Example usage

Suppose you want to automate download of extensions stored on AMO‘s sandbox, you must login first so this use-case is perfect for us.
Someone can consider this approach ugly, web services or Remora API are better but here I only want to demonstrate how to use nsIChannel.

Let’s start…

You can write the Javascript code shown below to download my extension RichFeedButton (dropped on sandbox an year ago)

var amoUsername = "dafi@localhost";
var amoPassword = "my_secret_code";

   "data[Login][email]=" + amoUsername + "&data[Login][password]" + amoPassword,



where our downloadProtectedResource function signature is shown below

function downloadProtectedResource(loginUrl, postData, resourceUrl, destPathName) { ... }

Nothing special, simply we need to know the HTML input names used for userid and password (ie data[Login][email] and data[Login][password]) and pass them in postData argument.

The downloadProtectedResource interacts with nsIChannel (nsIHttpChannel) and other XPCOM object

function downloadProtectedResource(loginUrl, postData, resourceUrl, destPathName) {
   var httpChannel = makeHttpChannel(loginUrl); // create an object nsIHttpChannel
   var stream = makeStringStream(postData); // create an object nsIStringInputStream
   setChannelPostData(httpChannel, stream); // fill data using nsIUploadChannel

   // downloader saves data on disk
   var downloader = new Downloader(resourceUrl, destPathName);
   // make a login then passes cookies to downloader object
   var cookieListener = new CookieRetrieverListener(downloader);

   // start authentication and download
   httpChannel.asyncOpen(cookieListener, null);

The object Downloader and CookieRetrieverListener implement the nsIStreamListener interface.

The cookieListener after obtaining cookies aborts the operation because we don’t need all server output, then it calls the downloader.

function CookieRetrieverListener(downloader) {
   this.downloader = downloader;
   this.cookies = "";

CookieRetrieverListener.prototype = {
   onStartRequest: function(request, ctx) {
         var channel = request.QueryInterface(Components.interfaces.nsIHttpChannel);
         this.cookies = channel.getRequestHeader("Cookie");

         // no need more data
         throw Components.results.NS_ERROR_ABORT;

   onDataAvailable : function(request, context, inputStream, offset, count) {

   onStopRequest: function(request, ctx, status) {
      this.downloader.cookies = this.cookies;

Another way to use this code consists to download localizations from BabelZilla as shown below.
BabelZilla requires many parameters on query string ūüėē

var bzUsername = "dafi_duck";
var bzPassword = "my_secret_code";
var bzItemId = "88";
var bzExtId = "4432";
                        + "&option=ipblogin&task=login&0b14737c5ade1f7697a8f81b33b0bacf=1"
                        + "&option=com_frontpage&Itemid=1"
                        + "&username=" + bzUsername
                        + "&passwd=" + bzPassword,
                        + "&Itemid=" + bzItemId
                        + "&extension=" + bzExtId,


Accessing to cookies received from server requires to use nsIStreamListener available only in asynchronous open calls.

This needs to start the download only when cookies are surely retrieved, this is achieved using the nsIRequestObserver.onStopRequest, any better idea is very appreciated.

Complete code

The complete code contains a few of helper functions (reading binary stream, saving file) and is available on SVN, it’s ready to be executed as Komodo macro simply setting userid and password.

December 13, 2008

Komodo French Localization coming soon

Filed under: babelzilla,komodo,localization — dafi @ 10:56 am

This morning I’ve found a couple of emails coming from my BabelZilla forum subscriptions, after reading them I went to WTS homepage where I saw the image shown below.


Fantastic! The French localization is completed, 2016 strings translated!

When I asked to BabelZilla guys to localize Komodo I could not imagine they did it so quickly.

Many activities must be coordinated with ActiveState team

  • Remove all hardcoded strings (amazingly new code continues to be written with hard-coded strings!!! ūüėĮ ūüėē )
  • Establish a time deadline to froze new strings added to trunks (this can drastically simplify localization cycle)
  • Lang Pack release process and better integration with code development lifecycle

But be calm dafi ūüėõ … one step at a time

I want to say thanks to Goofy, the BabelZilla Super Mod, and Pedro the Italian localizer that helped me from the beginning, they helped me to “extract” hard-coded strings and prepare patches to submit.
A big “thank you” goes to all localizers (especially the French community) for dedicating time and passion to this work.

Technically speaking an installable French lang pack is already ready for KomodoEdit v5.0.2, French team are completing the quality test and everybody can help them.

Who want to see Komodo in French can register on Babelzilla and read more at Komodo Lang Pack forum thread…

… and Goofy has already localized in French my extension TabSwitcher

Blog at