Firefox 书签探索未超过 Javascript 的第一级
我已经编写了一些代码来探索我的 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;
I've written some code to explore my Firefox bookmarks but I only get the first level of bookmarks (i.e. I don't get the links in the folders).
e.g.
Search_engines /
- yahoo.com
google.com
In this example I have only access to Search_engines and google.com not yahoo.com
My function being recursive I don't know why this happens.
my code :
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个明显的错误是使用
toolbarFolder
作为起点 - 这只是书签工具栏。如果您想要所有书签(即书签菜单、书签工具栏和未排序的书签),您需要更改查询参数:另一个问题是在
nsINavHistoryResultNode
对象上获取childCount
属性 - 那里没有这样的属性。在访问 nsINavHistoryContainerResultNode 的属性之前,您需要显式或隐式地(通过instanceof)调用QueryInterface
。所以我会写:One obvious mistake is using
toolbarFolder
as a starting point - that's only the bookmarks toolbar. If you want all bookmarks (meaning bookmarks menu, bookmarks toolbar and unsorted bookmarks) you need to change query parameters:The other issue is getting
childCount
property on ansINavHistoryResultNode
object - there is no such property. Before you can access properties ofnsINavHistoryContainerResultNode
you need to callQueryInterface
, either explicitly or implicitly (viainstanceof
). So I would write:只是为了节省想要获取所有书签的人的时间,不要这样做
query.setFolders([
bookmarksService.bookmarksMenuFolder,
bookmarksService.toolbar文件夹,
bookmarksService.unfiledBookmarksFolder
], 3);
正如 Wladimir 所建议的,因为它不会为您带来所有书签,而是对文件夹进行相交,只为您提供放置在所有这些文件夹中的书签,如 Mozilla 文档中所写
(https://developer.mozilla.org/en/Querying_Places)
Just to save time to anyone who want to get all the bookmarks, do not do
query.setFolders([
bookmarksService.bookmarksMenuFolder,
bookmarksService.toolbarFolder,
bookmarksService.unfiledBookmarksFolder
], 3);
as suggested by Wladimir since it WON'T bring you all of the bookmarks but instead do an intersect of the folders and only give u the bookmarks which are placed at all of those folders, as written at Mozilla doc's
( https://developer.mozilla.org/en/Querying_Places )