我希望能够在树中的任何位置从根文件夹返回所有用户书签文件夹的数组。
这是我来过的最接近的,它不返回任何 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 和分隔符。如果您只需要书签文件夹和查询的列表(例如位置页面的左窗格),则可以使用此选项。忽略历史查询。默认为假。
但是设置这个会导致查询不返回任何结果:
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