bookmarks.getTree()
bookmarks.getTree()
返回一个数组,该数组每一项为bookmarks.BookmarkTreeNode
对象,作为书签树的根节点。
如果它们是文件夹的话,你可以通过其 children
属性及其后代的 children
属性递归地访问整个树。
这是一个异步的函数,返回 Promise
。
使用格式
var gettingTree = browser.bookmarks.getTree()
参数
无。
返回值
Promise
对象,该对象未来会得到一个填充代表根节点的 bookmarks.BookmarkTreeNode
对象的数组。
浏览器兼容性
BCD tables only load in the browser
示例
这个示例会打印出整个书签树:
function makeIndent(indentLength) {
return ".".repeat(indentLength);
}
function logItems(bookmarkItem, indent) {
if (bookmarkItem.url) {
console.log(makeIndent(indent) + bookmarkItem.url);
} else {
console.log(makeIndent(indent) + "Folder");
indent++;
}
if (bookmarkItem.children) {
for (child of bookmarkItem.children) {
logItems(child, indent);
}
}
indent--;
}
function logTree(bookmarkItems) {
logItems(bookmarkItems[0], 0);
}
function onRejected(error) {
console.log(`An error: ${error}`);
}
var gettingTree = browser.bookmarks.getTree();
gettingTree.then(logTree, onRejected);
备注: This API is based on Chromium's chrome.bookmarks
API. This documentation is derived from bookmarks.json
in the Chromium code.
Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.