Getting all folders for a bookmark

225 views
Skip to first unread message

Asad Dhamani

unread,
Mar 24, 2015, 9:34:10 PM3/24/15
to chromium-...@chromium.org
Hi.

I'm working on a Chrome extension, and I need to use the bookmarks API to get the folder structure for a bookmark. I'm able to get the first folder like so:

    chrome.bookmarks.get(bookmark, function(results) {
       
ParentNode = results[0];
        console
.log(ParentNode.title);
   
});


However, I need to be able to get all the enclosing folders. An ugly way of doing that would be this:

chrome.bookmarks.get(bookmark, function(results) {



           
ParentNode = results[0];

        console
.log(ParentNode.title);

        chrome
.bookmarks.get(ParentNode.parentId, function(results) {

           
ParentNode = results[0];

            console
.log(ParentNode.title);

            chrome
.bookmarks.get(ParentNode.parentId, function(results) {

               
ParentNode = results[0];

                console
.log(ParentNode.title);

                chrome
.bookmarks.get(ParentNode.parentId, function(results) {

                   
ParentNode = results[0];

                    console
.log(ParentNode.title);

               
});

           
});

       
});

 

   
});


But that wouldn't really work. I've tried a while loop:

var ParentNode;
function getTags(bookmark) {
    chrome.bookmarks.get(bookmark, function(results) {
        ParentNode = results[0];
        while (ParentNode.parentId !== "undefined") {
            chrome.bookmarks.get(ParentNode.parentId, function(result) {
                ParentNode = result[0];
                console.log(ParentNode.title);
            });
        }
    });
}
However, for some reason, the loop keeps going on and crashes my browser. What would be an easy/efficient way to get the entire folder hierarchy for a bookmark? 

Thanks.

Reilly Grant

unread,
Mar 25, 2015, 11:33:26 AM3/25/15
to Asad Dhamani, chromium-...@chromium.org

Callbacks are asynchronous. ParentNode is not modified by your loop and so it runs indefinitely. The callback passed to chrome.bookmarks.get will only get a chance to run once the loop completes and control passes back to the JavaScript event loop which never happens.


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/40aff076-b15d-4b62-8232-300c7c27e27b%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Asad Dhamani

unread,
Mar 26, 2015, 5:30:11 AM3/26/15
to chromium-...@chromium.org, dhama...@gmail.com
Alright, that makes sense, thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
Reply all
Reply to author
Forward
0 new messages