如果我尝试以下代码:
chrome.bookmarks.getTree(function(items) {
items.forEach(function(item) {
document.write(item.url);
});
});它返回undifined。但当我写道:
chrome.bookmarks.getRecent(20, function(items) {
items.forEach(function(item) {
document.write(item.url);
});
});它起作用了。
为什么会有所不同?
发布于 2012-04-24 13:44:06
chrome.bookmarks.getTree和chrome.bookmarks.getRecent都返回一个BookmarkTreeNodes数组,但BookmarkTreeNodes不一定有url属性。在getTree的情况下,树的顶部节点是文件夹,并且没有URL:

如果使用getTree,则必须使用每个节点的children数组递归地遍历树。了解每个书签都有一个children属性(如果它是一个文件夹)或一个url属性(如果它是一个实际的书签)是很有帮助的。尝试如下所示:
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); }
}发布于 2015-07-16 09:56:42
chrome.bookmarks需要权限才能使用。尝试在页面chrome://bookmarks中使用它。
https://stackoverflow.com/questions/10268776
复制相似问题