MozillaZine

Bookmark Changes in Firefox 4

Talk about add-ons and extension development.
TheBaker

User avatar
 
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post Posted December 23rd, 2010, 3:53 am

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: 2277
Joined: February 3rd, 2009, 6:29 pm

Post Posted December 23rd, 2010, 9:07 am

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

TheBaker

User avatar
 
Posts: 193
Joined: October 20th, 2004, 9:20 am

Post Posted December 23rd, 2010, 9:13 am

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.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 1 guest