Skip to content

Commit

Permalink
add utility functions for chrome.storage.sync
Browse files Browse the repository at this point in the history
  • Loading branch information
afoome committed Mar 16, 2022
1 parent 177e5d5 commit 118e872
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ChromeStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ------------------------------------------------------------------
// since chrome.storage.sync's get and set is easy to get confused with the key
// we offer such utilities to ease the pain.
// ------------------------------------------------------------------

export function set(key, value) {
chrome.storage.sync.set({[key]: value}, function () {
console.log(`store value: ${value} with key:${key} successfully.`)
})
}

// since it's async, so load is a proper func name to use.
export function load(key, callback) {
chrome.storage.sync.get([key], function (result) {
console.log(`get raw result: %o with key:${key}`, result)
let value = result[key]
console.log(`get value: ${value} with key:${key}`)
if (value) {
callback(value)
}
})
}

// just alias for load function.
export function get(key, callback) {
load(key, callback)
}

0 comments on commit 118e872

Please sign in to comment.