Last active
November 30, 2022 21:50
-
-
Save iArmanKarimi/1deca6188a6f63ad900ea4442a1c3d84 to your computer and use it in GitHub Desktop.
Chrome extension localstorage high-level
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
const storage = { | |
get: async function (key) { | |
return new Promise(resolve => { | |
chrome.storage.local.get(key, e => resolve(e[key])) | |
}) | |
}, | |
set: function (key, value) { | |
chrome.storage.local.set({ [key]: value }) | |
}, | |
clear: function () { | |
chrome.storage.local.clear() | |
}, | |
remove: function (keys) { | |
chrome.storage.local.remove(keys) | |
}, | |
} |
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
const storage = { | |
get: async function (key: string): Promise<any> { | |
return new Promise(resolve => { | |
chrome.storage.local.get(key, e => resolve(e[key])) | |
}) | |
}, | |
set: function (key: string, value: any) { | |
chrome.storage.local.set({ [key]: value }) | |
}, | |
clear: function () { | |
chrome.storage.local.clear() | |
}, | |
remove: function (keys: string | string[]) { | |
chrome.storage.local.remove(keys) | |
}, | |
} |
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
var storage = require('./LocalStorage') | |
//=// Usage //=// | |
storage.set('_TEST_', true) | |
storage.set('_OTHER_TEST_', 2) | |
if (await storage.get('_TEST_') !== true) { | |
throw 'Expected to get the previously saved value from local storage' | |
} | |
storage.remove('_TEST_') | |
storage.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment