-1

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";
}
aaaidan
  • 7,093
  • 8
  • 66
  • 102
  • 1
    Did you go through and hand-format that code? Please post the original code in, select it and press the `{}` button in the toolbar to format it as code. – Bojangles Mar 27 '12 at 20:45
  • The problem is tha this code gives me only the last URL from the bookmarks. – user1296544 Mar 27 '12 at 20:56
  • trying to edit the code but cannot... – user1296544 Mar 27 '12 at 20:57
  • @user1296544: You are looping over all of the bookmark nodes but assigning all of them to the same variable - what did you expect? – Wladimir Palant Mar 27 '12 at 21:00
  • I wanted to get all bookmarks in textbox – user1296544 Mar 27 '12 at 21:04
  • @user1296544 What Wladimir was saying is that in your loop you are assigning the values to the same variable. Meaning you are overriding the variable with the most recent url. So store the node before reassigning it and you should have all of the urls – StartingGroovy Mar 27 '12 at 21:17
  • What do you mean by "trying to edit the code but cannot"? Is the code in a read-only file or something!? – aaaidan Mar 27 '12 at 21:57

1 Answers1

0

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.

aaaidan
  • 7,093
  • 8
  • 66
  • 102
  • Gr8 aaaidan thx for help its working now but only from the folder unfiledBookmarksFolder.... now how i can connect the other folders to print them all together. As u seee i have commented the service for toolbarFolder and bookmarksMenuFolder but it dosnt give me ani urls and titles..thx To all sorry for my poor info about my problem but im first time here and my english is not so good as yours ..thx all for comments – user1296544 Mar 28 '12 at 08:16
  • Thanks to all good people :) problem solved with all bookmarks :))) – user1296544 Mar 28 '12 at 14:27
  • No problems user1296544. Great english isn't a requirement here, just take care to fully describe the situation. A question written with care will get much more help! Three basic things to include: say _what_ you did (usually the code), what you _expected_ to happen, and what _actually_ happened. – aaaidan Mar 29 '12 at 01:09