Search code examples
javascriptfirefoxbookmarks

getAllBookmarks in firefox


I'm trying to get the content from all the nodes in the bookmarks menu into textbox.value, but only the last bookmark appears. What am I doing wrong?

function AllBookmarks()
{
    var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                                   .getService(Components.interfaces.nsINavHistoryService);
    var options = historyService.getNewQueryOptions();
    var query = historyService.getNewQuery();
    var bookmarksService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
                                     .getService(Components.interfaces.nsINavBookmarksService);
    //var toolbarFolder = bookmarksService.toolbarFolder;
    //var bookmarksMenuFolder = bookmarksService.bookmarksMenuFolder;
    var unfiledBookmarksFolder = bookmarksService.unfiledBookmarksFolder;

    //query.setFolders([toolbarFolder], 1);
    //query.setFolders([bookmarksMenuFolder], 1);
    query.setFolders([unfiledBookmarksFolder], 1);

    var result = historyService.executeQuery(query, options);
    var rootNode = result.root;
    rootNode.containerOpen = true;

    // iterate over the immediate children of this folder
    for (var i = 0; i < rootNode.childCount; i ++) {
      var node = rootNode.getChild(i);
    }

    // close a container after using it!
    rootNode.containerOpen = false;
    var textbox = document.getElementById("MyExtension");
    var title= "Title: " + node.title; // shows the title of URL
    var url= "\nURL: " + node.uri; // shows the URL
    textbox.value = title + url + "\n";
}

Solution

  • In the loop commented as "iterate over the immediate children of this folder", you are probably looping over each of the bookmarks correctly, but you are not doing anything with the each node before moving on to the next. As a result, the node variable is set to the last node when you leave the loop.

    Also, you are assigning to textbox.value, rather than appending to it, so even if you were acting on the data for each node you would have clobbered it each time, resulting in only the data of the last node (the same outcome!). If you want to build up a string like that, you have to append to it, not assign to it. One way to do this is with the += operator.

    So, the last part of the code should be something like:

    var textbox = document.getElementById("MyExtension");
    
    // iterate over the immediate children of this folder
    for (var i = 0; i < rootNode.childCount; i ++) {
      var node = rootNode.getChild(i);
      var title = "Title: " + node.title; // gets the title of URL
      var url = "\nURL: " + node.uri; // gets the URL
      textbox.value += title + ": " + url + "\n"; // note the += (append) operator
    }
    // close a container after using it!
    rootNode.containerOpen = false;
    

    NB: In many other (stricter) languages, your posted code wouldn't compile because you're using the variable node outside of the "scope" (the braces) in which it was declared. It is a good rule of thumb to follow voluntarily though: violating this guideline often means you're making a mistake, or need to think more carefully about what you're doing. In this very case, it may have alerted you to the problem.