问题
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