This is an archived page. It's not actively maintained.



Support for extensions using XUL/XPCOM or the Add-on SDK was removed in Firefox 57, released November 2017. As there is no supported version of Firefox enabling these technologies, this page will be removed by December 2020.

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

Starting from Firefox 53, no new legacy add-ons will be accepted on (AMO) for desktop Firefox and Firefox for Android.

Starting from Firefox 57, only extensions developed using WebExtensions APIs will be supported on Desktop Firefox and Firefox for Android.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to use WebExtensions APIs if they can. See the "Compatibility Milestones" document for more information.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.


Firefox 2 and earlier

Creating a new bookmark

var win = myBrowser.contentWindow;

// Get the bookmarks service
const BMSVC = Components.classes[";1"]

// Create the bookmark
BMSVC.createBookmarkInContainer(win.document.title, // bookmark name
                                win.location.href.toString(), // URI of the bookmark
                                null, // shortcut
                                win.document.title, // description
                                win.document.characterSet, // charset
                                null, // postData
                                bookmarksService.getBookmarksToolbarFolder(), // bookmark folder
                                0); // index in the folder

Firefox 3

Firefox 3 provides a reworked set of API for working with History and Bookmarks. The documentation for the new API is available at Places.


The Places bookmarks service, provided by the nsINavBookmarksService interface, provides methods for creating, deleting, and manipulating bookmarks and bookmark folders. This article offers examples for how to perform common bookmark management tasks using the bookmarks service.

Initiating the bookmarks service

As is the case with nearly all interfaces, before you can use the bookmarks service, you need to get access to it:

var bmsvc = Components.classes[";1"]

Creating a bookmark folder

Creating a new bookmark folder is done using the nsINavBookmarksService.createFolder() method. For example, to create a new folder in the Bookmarks menu:

var menuFolder = bmsvc.bookmarksMenuFolder; // Bookmarks menu folder
var newFolderId = bmsvc.createFolder(menuFolder, "Folder name here", bmsvc.DEFAULT_INDEX);

This code locates the Bookmarks menu's folder, then creates a new folder named "Folder name here" in it. Specifying DEFAULT_INDEX as the index at which to insert the new folder places it at the end of the list.

You can easily change this code to insert the new folder into the bookmarks toolbar by changing bookmarksMenuFolder to another folder attribute.

Creating a new bookmark

To create a new bookmark, use the nsINavBookmarksService.insertBookmark() method. The URI for the bookmark needs to be specified using an nsIURI object.

var ios = Components.classes[";1"]
var uri = ios.newURI("", null, null);
var newBkmkId = bmsvc.insertBookmark(newFolderId, uri, bmsvc.DEFAULT_INDEX, "");

This example instantiates the nsIIOService and uses it to create an nsIURI referring to the Google web site, then calls nsINavBookmarksService.insertBookmark() to create a new bookmark to Google, placing it at the end of the bookmarks folder referenced by newBkmkId.

Finding bookmark items

If you know the URI of a site and wish to find all bookmarks that link to it, you can use the nsINavBookmarksService.getBookmarkIdsForURI() method.

var ios = Components.classes[";1"]
var uri = ios.newURI("", null, null);
var bookmarksArray = bmsvc.getBookmarkIdsForURI(uri, {});

After executing this code, the array bookmarksArray contains the IDs of all bookmarks that refer to the specified URI (in this case, "").

Manipulating existing items

There are a number of convenient methods you can use to make changes to existing bookmarks and bookmark folders. This section covers some of them.

The item title

To change the title of a bookmark or bookmark folder, you use the nsINavBookmarksService.setItemTitle() method.

bmsvc.setItemTitle(newBkmkId, "New title");

This sets the title of the item referenced by the ID newBkmkId to "New title".

You can fetch the current title of an item using the nsINavBookmarksService.getItemTitle() method:

var thisTitle = bmsvc.getItemTitle(newBkmkId);

This code will display an alert containing the title of the item referenced by the ID newBkmkId.

The item URI

Similarly, you can obtain the URI corresponding to a given bookmark item by calling the nsINavBookmarksService.getBookmarkURI() method.

var thisURI = bmsvc.getBookmarkURI(newBkmkId);

Assuming you've run all the code samples previous to this one, this will output "".

You can use the nsINavBookmarksService.changeBookmarkURI() method to update the URI for a given bookmark item:

var uri = ios.newURI("", null, null);
bmsvc.changeBookmarkURI(newBkmkId, uri);

This example changes the bookmark to refer to the Mozilla web site instead of Google.


Note: All annotations, tags, and so forth are kept when the bookmark's URI is changed.


Checking to see if a URI is bookmarked

If you want to see if a given URI is already bookmarked -- for example, to avoid creating a new bookmark for a site that's already bookmarked -- you can use the nsINavBookmarksService.isBookmarked() method.

var ios = Components.classes[";1"]
var uri = ios.newURI("", null, null);
if (!bmsvc.isBookmarked(uri)) {
  bmsvc.insertBookmark(bmsvc.toolbarFolder, uri, bmsvc.DEFAULT_INDEX, "Mozilla");

This example looks to see if the user already has a bookmark for the Mozilla web site, and, if not, creates one, adding it to the user's bookmarks toolbar.

Finding the folder containing an item

If you need to know what folder contains an item (this can be especially handy after using nsINavBookmarksService.getBookmarkIdsForURI() to find bookmarks for a given URI), you can use the nsINavBookmarksService.getFolderIdForItem() method.

var parentFolderId = bmsvc.getFolderIdForItem(newBkmkId);

Observing changes to bookmarks and tags

To set up an observer to listen for changes related to bookmarks, you will need to create an nsINavBookmarkObserver object and use the nsINavBookmarksService.addObserver() and nsINavBookmarksService.removeObserver() methods.  Note that this example takes advantage of XPCOMUtils to generate the nsISupports.QueryInterface() method.

// An nsINavBookmarkObserver
var myExt_bookmarkListener = {
  onBeginUpdateBatch: function() {},
  onEndUpdateBatch: function() {},
  onItemAdded: function(aItemId, aFolder, aIndex) {},
  onItemRemoved: function(aItemId, aFolder, aIndex) {},
  onItemChanged: function(aBookmarkId, aProperty, aIsAnnotationProperty, aValue) {
  onItemVisited: function(aBookmarkId, aVisitID, time) {},
  onItemMoved: function(aItemId, aOldParent, aOldIndex, aNewParent, aNewIndex) {},
  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsINavBookmarkObserver])
// An extension
var MyExtension = {
  // This function is called when my add-on is loaded
  onLoad: function() {
    bmsvc.addObserver(myExt_bookmarkListener, false);
  // This function is called when my add-on is unloaded
  onUnLoad: function() {
  doSomething: function() {
    alert("Did something.");

See also