问题描述
我试图在Chrome扩展程序中编写更好的书签管理器。问题是没有简单的例子(我可以找到)关于如何实际使用。
我看过示例源代码(当我/我的电脑除了提供一个搜索框以外没有任何操作,键入/输入和按回车键都没有做任何事情)并且找不到任何有用的东西。
我的终极版目标是做一个扩展,使我可以保存页面,稍后阅读,而不必必须在某处某个服务上注册帐户。所以我打算在根文件夹/其他书签中创建一个或两个书签文件夹 - 至少是未读页面文件夹。在该文件夹中,我将创建未读的书签。当用户将该项目标记为已读时,它将从该文件夹中删除。
这就是我想要做的...任何帮助都将不胜感激,即使它只是指向我一些很好的例子。
更新:
...<脚本>
function display(tree){
document.getElementById(Output)。innerHTML = tree;
}
函数start(){
chrome.bookmarks.getTree(display);
}
< / script>
< / head>
< body>
< h4 id =输出>< / h4>
< script>
start();
< / script>
...
显示 [object Object] code>,这表明(至少对我来说JavaScript有限的经验)存在一个对象。但如何访问该对象的成员?
将树更改为 tree.id 或任何其他看起来是参数的显示 undefined 。
目前,没有好方法在书签API中查找诸如其他书签或书签栏之类的文件夹。您必须遍历所有书签并查找哪个节点具有这些根文件夹并保存其书签ID。该错误已提交给。
根ID始终为0,当我的意思是0时,它对应于书签栏和其他书签。作为任何树结构,每个节点都有孩子。如果您想要获取一个文件夹下的所有书签,则可以使用getChildren API并递归获取每个节点(也可以迭代执行)。例如,以下代码将获得每个书签:
printBookmarks('0');
函数printBookmarks(id){
chrome.bookmarks.getChildren(id,function(children){
children.forEach(function(bookmark){
console。 debug(bookmark.title);
printBookmarks(bookmark.id);
});
});
}
现在,为什么我们必须为每次迭代调用API?他们是获取整棵树的API。如果您尝试了它,您会看到getTree中的每个节点都会有一个孩子列表。这是完美的:
chrome.bookmarks.getTree(function(bookmarks){
printBookmarks(bookmarks);
});
函数printBookmarks(书签){
bookmarks.forEach(函数(书签){
console.debug(bookmark.id +' - '+ bookmark.title +' - '' + bookmark.url);
if(bookmark.children)
printBookmark(bookmark.children);
});
$ b就是这样,你可以做所有这些迭代以及更好的表现,但你可以弄清楚:)注意,既然你想重做书签栏,你可以用扩展(即将)覆盖该页面:
如果你想展示一个漂亮的HTML书签树,你可以通过扩展我上面展示的getTree功能来接受父DOM。您可以执行以下操作。编辑代码以使用getTree或折叠所有内容,并使用getChildren并在请求时获取更多书签。
I'm trying to write a better bookmark manager in Chrome extensions. The problem is that there are no simple examples (that I can find) about how to actually use the bookmarks API.
I've looked at the example source (when I d/led and installed it on my computer it didn't do anything except provide a search box. Typing/typing and pressing return failed to do anything) and can't find anything useful.
My ultimate goal is to make an extension that allows me to save pages to come and read later without having to go sign up for an account on some service somewhere. So I plan to create either one or two bookmark folders in the root folder/other bookmarks - at minimum an "unread pages" folder. In that folder I'll create the unread bookmarks. When the user marks the item as read, it will be removed from that folder.
So that's what I'm trying to do... any help will be greatly appreciated, even if it's just pointing me to some good examples.
UPDATE:
...<script> function display(tree){ document.getElementById("Output").innerHTML = tree; } function start(){ chrome.bookmarks.getTree(display); } </script> </head> <body> <h4 id="Output"></h4> <script> start(); </script> ...That displays [object Object], that suggests (at least to me with a limited JavaScript experience) that an object exists. But how to access the members of that object?
Changing tree to tree.id or any other of what look to be parameters displays undefined.
解决方案Currently, there is no good way to find folders such as "Other Bookmarks" or "Bookmarks Bar" in the bookmarks API. You would have to iterate through all the bookmarks and find which node has those root folders and save its bookmark id. The bug is filed Issue 21330.
The root id is always 0, and when I mean 0, it corresponds to "Bookmarks bar" and "Other bookmarks". As any tree structure, each node has children. If you want to fetch all the bookmarks under one folder, you can use getChildren API and get every node recursively (you can do it iteratively too). For example, the following will get every single bookmark:
printBookmarks('0'); function printBookmarks(id) { chrome.bookmarks.getChildren(id, function(children) { children.forEach(function(bookmark) { console.debug(bookmark.title); printBookmarks(bookmark.id); }); }); }Now, why do we have to call the API for every iteration? Their is an API to get the whole Tree. If you tried it out, you will see that every node in the getTree will have a list of children. That is perfect:
chrome.bookmarks.getTree(function(bookmarks) { printBookmarks(bookmarks); }); function printBookmarks(bookmarks) { bookmarks.forEach(function(bookmark) { console.debug(bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url); if (bookmark.children) printBookmark(bookmark.children); }); }That is all, you can do all this iteratively as well which is better performance, but you can figure that out :) Note that since you want to redo the bookmarks bar, you can override that page in extensions (soon):http://code.google.com/chrome/extensions/override.html
If you want to show a nice HTML tree of your bookmarks, you can easily do that by extending the getTree functionality I showed above to accept a parent DOM. You can do something like this. Edit the code to make use of the getTree or collapse everything and make use of the getChildren and fetch more bookmarks if they request it.
这篇关于获取Google Chrome的根书签文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!