Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Bookmarks API -

I'm attempting to create a simple example that would just alert the first 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);
};
like image 694
Korak Avatar asked Apr 06 '11 17:04

Korak


People also ask

Is there an API for Chrome?

The Chrome Management API is a suite of services that allows administrators to programmatically view, manage, and get insights about policies and usage of ChromeOS devices and Chrome browsers in their organization.

Can a Chrome extension access bookmarks?

The Chrome extension allows you to view your existing bookmarks as well as allow to add new bookmarks from Chrome. Easy tool to keep bookmarks from all browsers in one place and access with extension. You can use their mobile app to access the bookmarked pages on the go anywhere.


1 Answers

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

    }
}
like image 104
serg Avatar answered Sep 29 '22 08:09

serg