Last active
June 17, 2020 21:24
-
-
Save shahriar-shojib/5669ea03d86368dcfd53c68a7d405ad3 to your computer and use it in GitHub Desktop.
Easy Access to chrome localStorage API for extensions
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
class Storage { | |
set(object) { | |
return new Promise((resolve, reject) => { | |
try { | |
chrome.storage.local.set(object, resolve); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
} | |
get(key) { | |
return new Promise((resolve, reject) => { | |
chrome.storage.local.get(key, data => { | |
if (Object.keys(data).length === 0 && data.constructor === Object) reject('no data returned by localStorage'); | |
resolve(data[key]); | |
}); | |
}); | |
} | |
} | |
const storage = new Storage(); | |
export default storage; | |
//How to use | |
storage.set({test:123}).catch(console.log) | |
storage.get('test').then(data => console.log(data)).catch(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment