admin管理员组文章数量:1352828
If I try the following code:
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);
});
});
it works.
Why does it differ?
If I try the following code:
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);
});
});
it works.
Why does it differ?
Share Improve this question edited Jul 16, 2015 at 9:18 Xan 77.7k18 gold badges197 silver badges217 bronze badges asked Apr 22, 2012 at 14:27 TomTom 7949 silver badges19 bronze badges3 Answers
Reset to default 10Both 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:
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); }
}
Before modifying your code try this!
Check if the extension has the persimo 'bookmarks'. You can see it in chrome://extensions/, 'Details' button, 'Permissions' section.
If it doesn't say 'Read and change favorites', then delete the Chrome extension and reload it unpackaged.
In my case, I loaded the extension and worked on it, updating it to incorporate the changes, then I realized that I hadn't requested the 'bookmarks' permission in the manifest.json, so I added it and updated the extension. Then chrome.bookmarks gave me 'undefined'.
It took me more than a couple of hours to realize that the manifest.json is not updated when you update the extension, it is only updated when you delete and reupload the extension.
D'oh!
chrome.bookmarks
needs permission to use. Try to use it in page
chrome://bookmarks
.
本文标签: javascriptWhy doesn39t quotchromebookmarksgetTreequot workStack Overflow
版权声明:本文标题:javascript - Why doesn't "chrome.bookmarks.getTree" work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743895926a2557782.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论