crossrider: store snapshot of bookmarks in local database and compare to current bookmarks list

不打扰是莪最后的温柔 提交于 2019-12-12 05:21:40

问题


How do I store a snapshot of the bookmarks list and then compare that to the current bookmars list every n amount of time with a conditional statement that outputs any bookmarks that have been added? I have been trying to do this to no avail.

It would be great if you could provide a code example but if you just want to explain it conceptually it would be fine.


回答1:


Conceptually, you can achieve your goal by storing the previous bookmarks list in the extension's local database using appAPI.db.async, comparing it to the current bookmark list obtained using appAPI.bookmarks.getTree, and sending it to your API server using appAPI.request.post.

You can use the following code in your background.js file as your starting point for handling the bookmarks lists and write your own comparison function (getChanges) as you require:

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);
});


来源:https://stackoverflow.com/questions/17416346/crossrider-store-snapshot-of-bookmarks-in-local-database-and-compare-to-current

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!