Traversing Bookmarks with WebExtensions

Hi, I’ve been trying to develop a small extension to process bookmarks in a given folder, but without a lot of luck.

Since I’m new to WebExtensions (and only having a little previous experience with the legacy type) I thought I would start with trying to simply print out the list of bookmarks to the console and go from there.

I’m using chome.bookmarks, but the getTree method doesn’t seem to work - no errors. This bit of code is run from the background script.

function test_click(tab) {

    console.log("Testing from background script!");
    
    // Testing Bookmarks
    var bookmarks = chrome.bookmarks;
    //var bookmarks = browser.bookmarks;
    bookmarks.getTree(function (bookmarks2) {
    
        console.log(JSON.stringify(bookmarks2));
    });
}

// Handle button click
chrome.browserAction.onClicked.addListener(test_click);

It’s triggered by an icon on the toolbar.

The first console log message output works, but nothing appears underneath it. When I debug it, it just skips through, as if no bookmarks were found, or something along those lines. But there are a lot of bookmarks and folders.

Other items of importance: The bookmarks permission is present in the manifest.json. If I don’t include this permission, I do get errors.

The same piece of code actually works ok in Chrome. Any ideas?

Regards,

Daniel

Try printing something like bookmarks2.length. Are you getting an empty array, undefined, something else? You should also check the Browser Console for any errors.

1 Like

I just tried making a new test extension with just the essentials included - and now it works.

I’m not really sure why it wasn’t working before… It must have been something ill-defined in the manifest.json file.

Thanks for the suggestion anyway!

Daniel

I have found that the callbacks should not be an inline function. Create a new function and put just the function name in the callback, and that seems to work for me.

I am having a similar issue. Have you figured out what was the problem? Specifically?