我希望能够在树中的任何位置从根文件夹返回所有用户书签文件夹的数组。
这是我最接近的RESULT_TYPE_FOLDER:
function find_folders()
{
var history = Cc["@mozilla.org/browser/nav-history-service;1"]
.getService(Ci.nsINavHistoryService);
var query = history.getNewQuery();
var options = history.getNewQueryOptions();
// Query users bookmarks, not history
options.queryType = options.QUERY_TYPE_BOOKMARKS;
// Execute the search and store results
var result = history.executeQuery(query, options);
// Open the root containerNode and open it
var resultContainerNode = result.root;
// OPEN resultContainerNode
resultContainerNode.containerOpen = true;
var folders = [];
// Search results are now child items of this container?
for (var i=0; i < resultContainerNode.childCount; ++i) {
var childNode = resultContainerNode.getChild(i);
if(childNode.type === childNode.RESULT_TYPE_FOLDER)
{
folders.push(childNode);
}
}
// CLOSE resultContainerNode
resultContainerNode.containerOpen = false;
return folders;
};
find_folders();
如果删除此结果类型检查,我会获得大量URI,一些RESULT_TYPE_QUERY,但没有RESULT_TYPE_FOLDER:
childNode.type: 0
childNode.type: RESULT_TYPE_QUERY
childNode.title: Most Visited
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: 0
childNode.type: RESULT_TYPE_QUERY
childNode.title: History
此处的文档:https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions#Query_type_constants 建议你可以设置:
excludeItems boolean 此选项排除所有URI和分隔符 书签查询。如果你只想要一个列表,就可以使用它 书签文件夹和查询(例如地点的左侧窗格) 页)。对历史记录的查询忽略。默认为false。
但设置此项会导致查询无法返回结果:
function find_folders()
{
var history = Cc["@mozilla.org/browser/nav-history-service;1"]
.getService(Ci.nsINavHistoryService);
var query = history.getNewQuery();
var options = history.getNewQueryOptions();
options.queryType = options.QUERY_TYPE_BOOKMARKS;
options.excludeItems = true; // Exclude everything that isn't a folder?
// Execute the search and store results
var result = history.executeQuery(query, options);
// Open the root containerNode and open it
var resultContainerNode = result.root;
// OPEN resultContainerNode
resultContainerNode.containerOpen = true;
var folders = [];
// Search results are now child items of this container?
for (var i=0; i < resultContainerNode.childCount; ++i) {
var childNode = resultContainerNode.getChild(i);
folders.push(childNode);
}
// CLOSE resultContainerNode
resultContainerNode.containerOpen = false;
return folders;
};
find_folders(); // Returns nothing :C
答案 0 :(得分:2)
excludeItems
才有效,例如
query.setFolders([PlacesUtils.toolbarFolderId, PlacesUtils.bookmarksMenuFolderId,
PlacesUtils.unfiledBookmarksFolderId], 1);
它也不会返回文件夹的平面列表,因此您必须遍历结果,以递归方式打开和收集内容。
如果您有一个很好的用例来获取平面文件夹列表,则可以自由提交增强错误。
最后,你将Places节点从结果中取出并将它们分离出来,这使得它们基本上成为僵尸对象,如果你创建自己的对象并添加你关心的东西会更好,比如{{1或其他任何东西(我刚刚发现了一个与此行为相关的错误,我将提交并修复它。)
答案 1 :(得分:1)
好吧,我在Firefox Places查询界面中遇到了一些错误和限制。
1 - (bug?)将query.setFolders([folder_array], 1)
设置为第二个参数(文件夹计数)到除1之外的任何内容都不会导致结果。将其设置为1只会搜索阵列中的第一个文件夹。
2 - (限制)excludeItems
仅在指定要搜索的文件夹时才有效:C
3 - (限制/错误?)query.setSearchTerm(term)
似乎与文件夹名称不匹配,因此我必须根据搜索字词手动检查它们:Q __
粗略的解决方法是创建要搜索的文件夹数组,然后分别搜索每个文件夹,连接结果,如下所示:
bookmarkr_utilities.find_folders = function(search_term)
{
bookmarkr_utilities.debug_log("find_folders()");
bookmarkr_utilities.folders = [];
var folders = [ bookmarkr_utilities.bmsvc_service().toolbarFolder
, bookmarkr_utilities.bmsvc_service().bookmarksMenuFolder
, bookmarkr_utilities.bmsvc_service().unfiledBookmarksFolder ];
for (var i = 0; i < folders.length; i++)
{
bookmarkr_utilities.folders = bookmarkr_utilities.folders.concat(bookmarkr_utilities.find_inside_folder(search_term, folders[i]));
}
return bookmarkr_utilities.folders;
};
帮助实际运行搜索的功能:
bookmarkr_utilities.find_inside_folder = function(search_term, search_folder)
{
var history = Cc["@mozilla.org/browser/nav-history-service;1"]
.getService(Ci.nsINavHistoryService);
var result_folders = [];
var query = history.getNewQuery();
query.setFolders([search_folder], 1);
var options = history.getNewQueryOptions();
options.excludeItems = true; // exclude everything except folders
// only works when folders are set
// Execute the search and store results
var result = history.executeQuery(query, options);
// Open the root containerNode and open it
var resultContainerNode = result.root;
resultContainerNode.containerOpen = true;
// Search results are now child items of this container?
bookmarkr_utilities.debug_log("resultContainerNode.childCount: " + resultContainerNode.childCount);
for (var i=0; i < resultContainerNode.childCount; ++i) {
var childNode = resultContainerNode.getChild(i);
if(childNode.type === childNode.RESULT_TYPE_FOLDER)
{
// HACK HACK HACK
if(search_term)
{
if(childNode.title.toLowerCase() == search_term.toLowerCase())
result_folders.push(childNode);
}
// END HACK HACK HACK OH GOD
else
{
result_folders.push(childNode);
}
}
}
// Close container
resultContainerNode.containerOpen = false;
return result_folders;
}