【发布时间】:2011-06-07 08:51:17
【问题描述】:
我编写了一些代码来探索我的 Firefox 书签,但我只获得了第一级书签(即我没有获得文件夹中的链接)。
例如
-
搜索引擎/
- yahoo.com
google.com
在这个例子中,我只能访问 Search_engines 和 google.com 而不是 yahoo.com
我的函数是递归的,我不知道为什么会这样。
我的代码:
function browse_bookmark_node(bookmark_node, array)
{
// We explore the bookmarks with this function
// iterate over the immediate children of this folder
for (var i = 0; i < bookmark_node.childCount; i ++) {
var node = bookmark_node.getChild(i);
if (node.type ==0) {
// the node is a link so we add it to the array
array.push(node.title);
} else if (node.type ==6) {
// the node is a folder so we explore it
browse_bookmark_node(node, array);
}
}
}
function wrapper_browse_bookmark_node(bookmark_node) {
// We use this function to wrapp the function browse_bookmark_node and keep track of the links
var array = [];
browse_bookmark_node(bookmark_node, array);
return array;
}
// All the code following is used to access firefox bookmarks and works fine
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 bookmarksMenuFolder = bookmarksService.bookmarksMenuFolder;
query.setFolders([bookmarksMenuFolder], 1);
var result = historyService.executeQuery(query, options);
var rootNode = result.root;
rootNode.containerOpen = true;
// The function call to explore the bookmarks
var links_array = wrapper_browse_bookmark_node(rootNode);
// close a container after using it!
rootNode.containerOpen = false;
【问题讨论】:
-
@Wladimir Palant 非常感谢您的回答。阅读我的代码后,我意识到其中有一个错字!
标签: javascript function recursion firefox-addon bookmarks