简体   繁体   中英

how can Firefox add-on access user's bookmarks folders

I'm writing some Firefox add-on code that manipulates the user's bookmarks.

I started with the "Searching Bookmarks" code from https://developer.mozilla.org/En/Places_Developer_Guide , and ended up writing the following code, which works...

var folders = [bookmarksService.bookmarksMenuFolder, bookmarksService.toolbarFolder, bookmarksService.unfiledBookmarksFolder];
var bookmarks = [];
for (var i = 0; i < 3; i++) {
    query.setFolders([folders[i]], 1);
    var result = historyService.executeQuery(query, options);
    var rootNode = result.root;
    rootNode.containerOpen = true;
    getNode(rootNode, bookmarks);
    rootNode.containerOpen = false;
}

The problem with this code is that it hard-codes the 3 default bookmark folders. I'd like the code to handle the case in which the user has created their own bookmark folders.

How can this code be changed so that it loops over all of the bookmark folders?

I think you're getting confused with the "folder" terminology here. The three hard-coded items you have in your code block are all you need. Any bookmarks the user creates will be located in one of these three places. You can see this in action by opening up the bookmarks editor in Firefox (Ctrl + Shift + B). In the tree pane on the left, select the All Bookmarks item, and note that there are only 3 (possibly 4) items underneath it:

  1. Bookmarks Toolbar
  2. Bookmarks Menu
  3. Unsorted Bookmarks

If you right-click the "All Bookmarks" top-level item, you'll note that there is no "Create Folder" option at this level. Any user-created bookmarks are below the sub-items listed at this level.

The Places Developer Guide lists one additional top-level folder ( tagsFolder ), but I don't think you need to worry about that one. I can't imagine a bookmark existing there and not in one of the other three locations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM