-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add utility functions for chrome.storage.sync
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |