Skip to content

Instantly share code, notes, and snippets.

@janbaykara
Created April 13, 2015 10:43
Show Gist options
  • Save janbaykara/adb1afe593545f4d662f to your computer and use it in GitHub Desktop.
Save janbaykara/adb1afe593545f4d662f to your computer and use it in GitHub Desktop.
A little class for syncing and accessing chrome.sync() data functionally.
// Class for simultaneously pushing/retrieving from foreign chrome.sync and local object.
var Storage = function() {
this.set = function(property,value,cb) {
this[property] = value
chrome.storage.sync.set(
{property: value},
function sentToStorage(storage) {
console.log("Set new "+property+" = "+value)
if(typeof cb === 'function') cb()
}
)
return this[property]
}
this.update = function(property,cb) {
var that = this
chrome.storage.sync.get(property, function receivedPropertyFromStorage(storage) {
console.log("New "+property+" = "+storage[property])
that[property] = storage[property]
if(typeof cb === 'function') cb(that[property])
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment