开发者

Chrome Bookmarks API -

I'm attempting to create a simple example that would just alert the f开发者_如何学运维irst 5 bookmark titles.

I took Google's example code and stripped out the search query to see if I could create a basic way to cycle through all Nodes. The following test code fails my alert test and I do not know why.

function dumpBookmarks() {
var bookmarkTreeNodes = chrome.bookmarks.getTree(
  function(bookmarkTreeNodes) {
   (dumpTreeNodes(bookmarkTreeNodes));
  });
}
function dumpTreeNodes(bookmarkNodes) {
var i;
for (i = 0; i < 5; i++) {
  (dumpNode(bookmarkNodes[i]));
}
}
function dumpNode(bookmarkNode) {
alert(bookmarkNode.title);
};


Just dump your bookmarkTreeNodes into the console and you will see right away what is the problem:

var bookmarkTreeNodes = chrome.bookmarks.getTree(
  function(bookmarkTreeNodes) {
   console.log(bookmarkTreeNodes);
  });
}

(to access the console go to chrome://extensions/ and click on background.html link)

As you would see a returned tree contains one root element with empty title. You would need to traverse its children to get to the actual bookmarks.

Simple bookmark traversal (just goes through all nodes):

function traverseBookmarks(bookmarkTreeNodes) {
    for(var i=0;i<bookmarkTreeNodes.length;i++) {
        console.log(bookmarkTreeNodes[i].title, bookmarkTreeNodes[i].url ? bookmarkTreeNodes[i].url : "[Folder]");

        if(bookmarkTreeNodes[i].children) {
            traverseBookmarks(bookmarkTreeNodes[i].children);
        } 

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜