Firefox书签探索不会超过Java的第一级
作者:互联网
我已经编写了一些代码来浏览我的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;
解决方法:
一个明显的错误是使用toolbarFolder作为起点,这只是书签工具栏.如果要使用所有书签(即书签菜单,书签工具栏和未排序的书签),则需要更改查询参数:
query.setFolders([
bookmarksService.bookmarksMenuFolder,
bookmarksService.toolbarFolder,
bookmarksService.unfiledBookmarksFolder
], 3);
另一个问题是在nsINavHistoryResultNode对象上获取childCount属性-没有此类属性.在访问nsINavHistoryContainerResultNode的属性之前,需要显式或隐式(通过instanceof)调用QueryInterface.所以我会写:
} else if (node.type == 6 && node instanceof Components.interfaces.nsINavHistoryContainerResultNode) {
var oldOpen = node.containerOpen;
node.containerOpen = true;
browse_bookmark_node(node, array);
node.containerOpen = oldOpen;
}
标签:bookmarks,javascript,function,recursion,firefox-addon 来源: https://codeday.me/bug/20191009/1877696.html