pinoyyid
pinoyyid

Reputation: 22306

how to identifying a deleted bookmark

In my extension, I have a listener on the chrome.bookmarks.onRemoved event.

When my listener is called, it's passed a parent and an index, rather than an image of the deleted bookmark. What I can't figure out is how do I find out which bookmark was deleted?

Upvotes: 0

Views: 161

Answers (2)

Sungguk Lim
Sungguk Lim

Reputation: 6228

Happy news.

chrome.bookmarks.onRemoved callback has 'node' param. it includes the details of removed bookmark node.

You can use it from Chrome canary Version 48.0.2529.0.

Upvotes: 1

Métoule
Métoule

Reputation: 14472

You can't use the chrome.bookmarks.get API to get the removed bookmark, since it's been removed. The only solution I can think of is to keep a copy of the full bookmark tree, and to search the id of the removed bookmark. A naive implementation would be:

var bookmarks = [];

function updateBookmarks()
{
    chrome.bookmarks.getTree(function(results) { bookmarks = results; });
}
updateBookmarks();

chrome.bookmarks.onRemoved.addListener(function(id, removeInfo)
{
    console.log("Removed bookmark");
    console.log(findBookmarkWithId(bookmarks, id));

    updateBookmarks();
});

function findBookmarkWithId(bookmarks, id)
{
    if (bookmarks === null || typeof bookmarks === "undefined")
        return null;

    for (var i = 0; i < bookmarks.length; i++) 
    {
        if (bookmarks[i].id === id)
            return bookmarks[i];

        var child = findBookmarkWithId(bookmarks[i].children, id)
        if (child !== null)
            return child;
    }

    return null;
}

// keep local copy up to date 
chrome.bookmarks.onCreated.addListener(function(id, bookmark)
{
    updateBookmarks();
});

chrome.bookmarks.onChanged.addListener(function(id, bookmark)
{
    updateBookmarks();
});

// TO DO: deal with chrome.bookmarks.onImportBegan / onImportEnd

Upvotes: 0

Related Questions