Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sync chrome bookmarks with server in extension using API

I am creating a Chrome extension that allows you to view, update, create, and remove your Chrome bookmarks. I would also like to be able load and save a user's bookmarks through our server instead of syncing through a google account. The problem I'm having is that I'm not sure how we would sync these bookmarks across devices. Since the Chrome bookmarks API does not have any method(s) to load/sync a tree, I figure I will save each bookmark/folder in an array on the server such as

[{id: "", parentId: "", index: "", title: "", url: ""}, ...]

When a user logs into their account on a new device/browser, I obtain the bookmark/folder array from the server and loop through them. In the loop, I would use Chrome.bookmarks.get with the ID to check if that bookmark already exists, and if it does not, then use the create function to create that bookmark. Problem is, there is no way that I can see in the documentation to specify the ID of the newly created bookmark/folder. What if the bookmark/folder created on a new device/browser has a different ID then the original device/browser? Then using the get function (which requires you specify the ID you are looking for) would not allow me to compare bookmarks correctly. Is there another solution for this problem?

like image 667
CaitlinHavener Avatar asked Oct 29 '25 15:10

CaitlinHavener


1 Answers

I actually ended up using the Chrome bookmarks search function to search for the bookmark by url and title, then matching to see if the parent ID matched. Kind of a screwy way to do this, but it works!

function initBookmarks(){
  //hit endpoint to get array of current bookmarks
  var curBookmarks = [
    {
      id : "7",
      parentId : "1",
      index : 12,
      title : "A programmatically generated bookmark",
      url : "http://google.com"
    },
    {
      id : "8",
      parentId : "1",
      index : 13,
      title : "A programmatically generated bookmark 2",
      url : "http://google.com"
    }
  ];

  if(curBookmarks.length > 0){
    curBookmarks.forEach(function(bookmark,index){
      console.log("searching.. ", {url : bookmark.url, title : bookmark.title});
      chrome.bookmarks.search({url : bookmark.url, title : bookmark.title}, function(result){
        console.log('result for search for is', result, bookmark.title);
        var flagExists = false;
        for(var i = 0; i < result.length; i++){
          if(result[i].parentId === bookmark.parentId) flagExists = true;
        }

        if(!flagExists){
          chrome.bookmarks.create({
            parentId: bookmark.parentId,
            index: bookmark.index,
            title: bookmark.title,
            url: bookmark.url
          }, function(){
            console.log('IVE CREATED!!!');
            if(index === curBookmarks.length-1)
              chrome.bookmarks.getTree(parseBookmarks);
          });
        }
        else{
          if(index === curBookmarks.length-1)
            chrome.bookmarks.getTree(parseBookmarks);
        }
      })
    })
  }
  else{
    chrome.bookmarks.getTree(parseBookmarks);
  }

}
like image 93
CaitlinHavener Avatar answered Oct 31 '25 06:10

CaitlinHavener