user1178020
user1178020

Reputation: 31

Unable to set firefox bookmark description for in Add-On Kit API

I found the nsINavBookmarksService, however since Firefox 3, it does not seem to have any API methods for getting/setting the Bookmark description. (API doc)

I've seen other Add-Ons modify the description as a method of synchronized data storage (which is exactly what I'm trying to do). I'm guessing perhaps the description is a non-gecko standard, and that's why it is not directly supported, but then there must be a completely different interface for manipulating Bookmarks that I haven't discovered.

Can anyone help with this newbie problem?

Upvotes: 2

Views: 206

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

Starting with Firefox 3 the bookmarks have been merged into the Places database containing all of your browsing history. So to get the bookmarks you do a history query, like this (first line is specific to the Add-on SDK which you appear to be using):

var {Cc, Ci} = require("chrome");
var historyService = Cc["@mozilla.org/browser/nav-history-service;1"]
                       .getService(Ci.nsINavHistoryService);
var options = historyService.getNewQueryOptions();
var query = historyService.getNewQuery();
query.onlyBookmarked = true;

var result = historyService.executeQuery(query, options);
result.root.containerOpen = true;
for (var i = 0; i < result.root.childCount; i++)
{
  var node = result.root.getChild(i);
  console.log(node.title);
}

That's mostly identical to the code example here. Your problem is of course that nsINavHistoryResultNode has no way of storing data like a description. You can set annotations however, see Using the Places annotation service. So if you already have a node variable for your bookmark:

var annotationName = "my.extension.example.com/bookmarkDescription";
var ioService = Cc["@mozilla.org/network/io-service;1"]
                  .getService(Ci.nsIIOService);
var uri = ioService.newURI(node.uri, null, null);
var annotationService = Cc["@mozilla.org/browser/annotation-service;1"]
                          .getService(Ci.nsIAnnotationService);
annotationService.setPageAnnotation(uri, annotationName,
     "Some description", 0, Ci.nsIAnnotationService.EXPIRE_NEVER);

For reference: nsIAnnotationService.setPageAnnotation()

Upvotes: 1

Related Questions