Bookmark Changes in Firefox 4

Talk about add-ons and extension development.
Post Reply
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Bookmark Changes in Firefox 4

Post by TheBaker »

I'm trying to update my extension to Firefox 4, but have hit a small 'roadblock' with some of the changes that have been made to the bookmarks API.

The old version used getChildFolder() to retrieve a folder with a known name, but it has been made obsolete as of Gecko 2 (Firefox 4): https://developer.mozilla.org/en/XPCOM_ ... ildFolder()

Could anyone tell me the 'new' way of retrieving a folder ID for a folder with a known title?
morat
Posts: 6683
Joined: February 3rd, 2009, 6:29 pm

Re: Bookmark Changes in Firefox 4

Post by morat »

I don't know if there is an easier way...

Code: Select all

var bmService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].
  getService(Components.interfaces.nsINavBookmarksService);
var folder;
folder = bmService.bookmarksMenuFolder;
folder = bmService.getChildFolder(folder, "Example");
alert(folder);

Code: Select all

function getChildFolder(aFolder, aSubFolder) {
  var htService = Components.classes["@mozilla.org/browser/nav-history-service;1"].
    getService(Components.interfaces.nsINavHistoryService);
  var query = htService.getNewQuery();
  var options = htService.getNewQueryOptions();
  query.setFolders([aFolder], 1);
  var result = htService.executeQuery(query, options);
  var rootNode = result.root;
  var childFolder = 0;
  rootNode.containerOpen = true;
  for (var i = 0; i < rootNode.childCount; i++) {
    var node = rootNode.getChild(i);
    if (node.type == node.RESULT_TYPE_FOLDER && node.title == aSubFolder) {
      childFolder = node.itemId;
      break;
    }
  }
  rootNode.containerOpen = false;
  return childFolder;
}
var bmService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].
  getService(Components.interfaces.nsINavBookmarksService);
var folder;
folder = bmService.bookmarksMenuFolder;
folder = getChildFolder(folder, "Example");
alert(folder);

https://developer.mozilla.org/en/nsINav ... ResultNode
User avatar
TheBaker
Posts: 193
Joined: October 20th, 2004, 9:20 am

Re: Bookmark Changes in Firefox 4

Post by TheBaker »

Thanks morat, I came up with a similar solution about 20 minutes ago that seems to work. Whether it's the best way of doing it or not, I don't know - but it works so I'll take it.
Post Reply