Using the FormHistory Module

Mar 4, 2014

In recent times, Mozilla has deprecated the nsIFormHistory2 and nsIFormHistory interfaces (see bug 878677 for more information), replacing both with the FormHistory.jsm module. This module provides an asynchronous way to store form history items, which is good for performance. Like many of the Mozilla interfaces, however, documentation is nearly non-existent. I like learning by example, and I've figured out how the FormHistory module works. Here are a few examples showing how to use it:

// Import the module
Components.utils.import('resource://gre/modules/FormHistory.jsm');

// Remove all stored history for a specific field
// ('GBL-Search-History' in this example)
FormHistory.update({op: "remove", fieldname: "GBL-Search-History"});

// Add specific terms to a specific field
FormHistory.update({op: "bump", fieldname: "GBL-Search-History", 
                    value: termsToStore});

Update: This article previously indicated to use the add operation to add a term to a specific field. That function, however, will result in duplicate entries as of this writing. The bump operation is now what I recommend to use. End Update

As you can see here, the update() function is the one I most care about. This function has several operations (specified by the "op:" property above) available to it:

  • add
  • update
  • remove
  • bump

The header comment in the FormHistory.jsm file details the specifics for these operations, as well as other functions. Hopefully these simple examples will help someone out. It wasn't immediately clear to me how to use this module when I first switched over.

No comments (yet!)

Leave a Comment

Ignore this field:
Never displayed
Leave this blank:
Optional; will not be indexed
Ignore this field:
Both Markdown and a limited set of HTML tags are supported
Leave this empty: