chrome的扩展中怎么调用书签的API?

想知道怎么调用chrome的API打印出全部的书签,怎么判断一个节点是书签还是文件夹?
希望给出具体的代码,并带上解释。

1 个回答

1,chrome.bookmarks.getTree(function callback)

Retrieves the entire Bookmarks hierarchy.
Parameters
callback ( function )
Callback function
The callback parameter should specify a function that looks like this:
function(array of BookmarkTreeNode results) {...};
results ( array of BookmarkTreeNode )

获得 一个BookmarkTreeNode类型的Arrary

2,如果BookmarkTreeNode节点的children 属性为空,则说明是一个空文件夹或者是一个书签;
url 属性为空,则说明是一个文件夹,下面是原文:

(The URL navigated to when a user clicks the bookmark. Omitted for folders.)

<html>
<head>
<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" rel="stylesheet">
<style>
div, td, th { color: black; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script>

  function dumpBookmarks(query) {
    var bookmarkTreeNodes = chrome.bookmarks.getTree(
      function(bookmarkTreeNodes) {
        $('#bookmarks').append(dumpTreeNodes(bookmarkTreeNodes, query));
      });
  }
  function dumpTreeNodes(bookmarkNodes, query) {
    var list = $('<ul>');
    var i;
    for (i = 0; i < bookmarkNodes.length; i++) {
      list.append(dumpNode(bookmarkNodes[i], query));
    }
    return list;
  }
  function dumpNode(bookmarkNode, query) {
    if (bookmarkNode.title) {
      if (query && !bookmarkNode.children) {
        if (String(bookmarkNode.title).indexOf(query) == -1) {
          return $('<span></span>');
        }
      }
     var anchor = $('<a>');
      anchor.attr('href', bookmarkNode.url);
      anchor.text(bookmarkNode.title);      
      anchor.click(function() {
        chrome.tabs.create({url: bookmarkNode.url});
      });
    }
    var li = $(bookmarkNode.title ? '<li>' : '<div>').append(anchor);
    if (bookmarkNode.children && bookmarkNode.children.length > 0) {
      li.append(dumpTreeNodes(bookmarkNode.children, query));
    }
    return li;
  }
</script>
</head>
<body onload="dumpBookmarks();" style="width: 400px">

<div id="bookmarks"></div>

</body>
</html>


{
  "name": "Bookmarks",
  "version": "1.0",
  "description": "A browser action with a popup dump of all bookmarks",
  "permissions": [
    "bookmarks", "tabs"
  ],
  "browser_action": {
      "default_title": "My Bookmarks.",
      "default_icon": "icon.png",
      "popup": "popup.html"
  }
}

你的回答