Chrome书签API- [英] Chrome Bookmarks API -

查看:108
本文介绍了Chrome书签API-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的示例,该示例将仅警告前5个书签标题.

I'm attempting to create a simple example that would just alert the first 5 bookmark titles.

我采用了Google的示例代码,并删除了搜索查询,以查看是否可以创建一种循环遍历所有节点的基本方法.以下测试代码未通过我的警报测试,我也不知道为什么.

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);
};

推荐答案

只需将您的 bookmarkTreeNodes 转储到控制台中,您将立即看到问题所在:

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);
  });
}

(要访问控制台,请转到 chrome://extensions/,然后单击 background.html 链接)

(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);
        } 

    }
}

这篇关于Chrome书签API-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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