Latest Tweets

ICEWEASEL 3.0.6: Exporting our bookmarks to HTML at runtime

The issue

Iceweasel 3.0.X – and, in fact, Firefox 3.0.X -, does not allow to export our bookmarks directly to the bookmarks.html file through the visual icons or menus available in the menubar. In pevious code branches, that is: 2.X and 1.X, this functionality was available.

First approach

Reading some documentation on the MDC website, one can find out there’s nothing we can do about it because of the fact that this aid was removed starting from the 3.0.X code branch. However, the MDC’s team allows mozilla users to activate, if they are in need of doing so, the non-visual browser.bookmarks.autoExportHTML option, setting this one to “TRUE”. In this case, when we close the browser, all our bookmarks will be saved to the classic bookmarks.html file, as before.

This way, we could achieve what we were looking for, but, unluckily, we could not see all our bookmarks added, removed, modified or whatever at runtime. It was something really annoying, so we had to find out how we could alter Iceweasel code so as to allow bookmarks to be saved as long as we managed them.

Looking for sources

Taking a look at the Iceweasel distribution files, installed using apt-get on any Debian GNU/Linux system, we found that some browser issues could be controlled using JavaScript files. We could modify them with no need of recompiling the browser, of course. This way, we had to find out where we could find the methods, functions, routines, etcetera, managing bookmarks and interacting with the user graphical interface – that is, the menu-bars and icons for controlling all tasks related to the bookmarks -.

All the code in charge of exporting the bookmarks to HTML file was found here: browser/components/nsBrowserGlue.js. At a glance, its quite easy to understand:

  _shutdownPlaces: function bg__shutdownPlaces() {
    // Backup and archive Places bookmarks.
    this._archiveBookmarks();
 
    // Backup bookmarks to bookmarks.html to support apps that depend
    // on the legacy format.
    var prefs = Cc["@mozilla.org/preferences-service;1"].
                getService(Ci.nsIPrefBranch);
    var autoExportHTML = false;
    try {
      autoExportHTML = prefs.getBoolPref("browser.bookmarks.autoExportHTML");
    } catch(ex) {
      Components.utils.reportError(ex);
    }
 
    /* TCG:
        this line code exports all bookmarks to html file
    */
    if (autoExportHTML) {
      Cc["@mozilla.org/browser/places/import-export-service;1"].
        getService(Ci.nsIPlacesImportExportService).
        backupBookmarksFile();
    }
  },

Well, as we can see in the previous code snippet, all we needed to do was add this code line somewhere in the Icewasel JScript files in charge of managing the bookmarks, well-known as “places” in MDC terminology.

Altering the code

All JScript code can be debugged using the “Error console”. This is true for all JSCript files concerning the internal behavior of Iceweasel, also, which, in turn, is quite helpful.

Reading all the code, we found where the bookmarks were managed, in the JScript code file named components/nsPlacesTransactionsService.js. In this file, there are a lot of methods, all of them direclty related to some common tasks, like creating new folders – createFolder(); -, adding a new bookmark – addItem(); -, removing one – removeItem(); -, altering the uri’s description, and so on.

Thus, we added the line code shown below in all of these methods:

       Cc["@mozilla.org/browser/places/import-export-service;1"].
                getService(Ci.nsIPlacesImportExportService).
                backupBookmarksFile();

For example, when we add a new item to the bookmarks database:

[...]
placesCreateItemTransactions.prototype = {
[...]
    // TCG:
    //  Added my code right here:
    Cc["@mozilla.org/browser/places/import-export-service;1"].
                 getService(Ci.nsIPlacesImportExportService).backupBookmarksFile();
}
[...]

And that’s all!

Conclusions

In order to use my code changes, all you need is the altered JScript file named nsPlacesTransactionsService.js, replacing the existing one with a simple copy operation. There’s no need of recompiling the browser, but I suggest you to close it before copying this file. This patch has been tested on Iceweasel 3.0.6 on a Debian GNU/Linux Lenny EM64T distro.

You can download my “patch” RIGHT HERE.

There’s a demonstrative video showing how my patch works right HERE!.