I’d like to be able to return an array of all a users bookmark folders from the root folder at any point in the tree.
This is the closest I’ve come which returns nothing of 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();
If I remove this result type checking I get lots of URIs, some RESULT_TYPE_QUERYs, but no 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
The documentation here: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions#Query_type_constants
suggests you can set:
excludeItems boolean This option excludes all URIs and separators from
a bookmarks query. This would be used if you just wanted a list of
bookmark folders and queries (such as the left pane of the places
page). Ignored for queries over history. Defaults to false.
But setting this causes the query to return no results:
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
Okay looks like I’ve run into a few bugs and limitations in the Firefox Places query interface.
1 – (bug?) setting
query.setFolders([folder_array], 1)with the second paramater (folder count) to anything other than 1 causes no results. Setting it to 1 will only search the first folder from your array.2 – (limitation)
excludeItemsworks only if you specify folders to search into which isn’t helpful :C3 – (limitation/bug?)
query.setSearchTerm(term)seems to not match folder names so I had to manually check them against the search term :Q__Sketchy workaround solution is to create the array of folders to search and then search separately through each one, concatenating the results, like so:
Helper function to actually run the search: