Created
May 21, 2020 09:27
-
-
Save seunggabi/ad1e1fb02527a5ba91d70752371ef500 to your computer and use it in GitHub Desktop.
db.js
This file contains hidden or 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
// menifest.json | |
// "permissions": [ | |
// "storage" | |
// ], | |
window.$seunggabi = { | |
db: (function () { | |
let name = getName(); | |
let type = getType(); | |
function getName() { | |
return chrome.runtime.getManifest().name.toUpperCase(); | |
} | |
function getType() { | |
return 'local'; | |
} | |
function getStorage() { | |
return new Promise((resolve) => { | |
try { | |
chrome.storage[type].get(name, (storage = {}) => { | |
resolve(storage[name] || {}); | |
}); | |
} catch(e) { | |
resolve(); | |
} | |
}); | |
} | |
function setStorage(data) { | |
return new Promise((resolve) => { | |
let obj = {}; | |
obj[name] = data; | |
chrome.storage[type].set(obj, () => { | |
resolve(); | |
}); | |
}); | |
} | |
function put(key, value) { | |
return new Promise((resolve) => { | |
getStorage().then((storage = {}) => { | |
storage[key] = value; | |
setStorage(storage).then(() => { | |
resolve(storage); | |
}); | |
}); | |
}); | |
} | |
function get(key) { | |
return new Promise((resolve) => { | |
getStorage().then((storage = {}) => { | |
resolve(storage[key]); | |
}); | |
}); | |
} | |
return { | |
put, | |
get | |
}; | |
})(), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment