为什么不"chrome.bookmarks.getTree"?工作? [英] Why doesn't "chrome.bookmarks.getTree" work?

查看:78
本文介绍了为什么不"chrome.bookmarks.getTree"?工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试以下代码:

chrome.bookmarks.getTree(function(items) {
  items.forEach(function(item) {
    document.write(item.url);
  });
});

它返回未定义的.但是当我写:

it returns undifined. But when I write:

chrome.bookmarks.getRecent(20, function(items) {
  items.forEach(function(item) {
    document.write(item.url);
  });
});

有效.

为什么有区别?

推荐答案

chrome.bookmarks.getTree chrome.bookmarks.getRecent 均返回 BookmarkTreeNodes ,但是BookmarkTreeNodes不一定具有 url 属性.对于 getTree ,树的顶部节点是文件夹,没有URL:

Both chrome.bookmarks.getTree and chrome.bookmarks.getRecent return an array of BookmarkTreeNodes, but BookmarkTreeNodes do not necessarily have a url property. In the case of getTree, the top nodes of the tree are folders and do not have URLs:

如果使用 getTree ,则必须使用每个节点的 children 数组递归遍历树.有助于了解每个BookmarkTreeNode 都有一个 children 属性(如果是文件夹)或一个 url 属性(如果是实际书签).尝试类似的东西:

If you use getTree, you'll have to traverse the tree recursively using each node's children array. It helps to know that every BookmarkTreeNode either has a children attribute (if it's a folder) or a url attribute (if it's an actual bookmark). Try something like:

chrome.bookmarks.getTree(function(itemTree){
    itemTree.forEach(function(item){
        processNode(item);
    });
});

function processNode(node) {
    // recursively process child nodes
    if(node.children) {
        node.children.forEach(function(child) { processNode(child); });
    }

    // print leaf nodes URLs to console
    if(node.url) { console.log(node.url); }
}

这篇关于为什么不"chrome.bookmarks.getTree"?工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆