Question

The code below belongs to a crossrider extension I am currently attempting to develop that hopefully will sync bookmarks across browsers. This file is currently the background.js file it will first retrieve a snapshot of the bookmarks file from the local database then compare that to the current list of bookmarks and if there are any differences (either additions to the bookmarks list or subtractions) they will be be returned with the getChanges() function and then sent to the server, finally the script updates the snapshot and waits 30 seconds before restarting the process. I dont really know how to make the getChanges() function. It needs to return presumably a json object indicating both the additions and subtractions (both their titles and urls). If someone could write the code for the function that would be great. Thanks

appAPI.ready(function() {
// Poll every 30 seconds
setInterval(function() {
  appAPI.db.async.get('prevBookmarks', function(value) {
  // Load or initialize previous bookmarks list
  var prevBookmarks = (value) ? value : {};

  // Get current bookmarks
  appAPI.bookmarks.getTree(function(nodes) {
    // Save bookmark list for next comparison
    appAPI.db.async.set('prevBookmarks', nodes);

    // In your getChanges functions, traverse the bookmark trees collating
    // changes and then post then to your API server using appAPI.request
    var changes = getChanges(prevBookmarks, nodes);
    appAPI.request.post({
      url: http://yourAPIserver.com,
      postData: changes,
      contentType: 'application/json'
    });
  });
});
}, 30 * 1000);
});
Was it helpful?

Solution

Ok, you've got jQuery as one of your tags, so try this link: Compare 2 arrays which returns difference.

It returns the differences between two arrays. You'll have to perform this twice for what you're doing, once to figure out what is in current that is not in previous and vice versa. I don't know what properties are contained in your bookmarks so this simple example might not exactly suit your needs, but it might point you in the right direction.

Good luck and welcome to JavaScript!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top