Created
August 8, 2014 07:51
-
-
Save yurtaev/d2ce7c1165c417d0cf0e to your computer and use it in GitHub Desktop.
Simple localStorage function with Storage API fallback for chrome apps.
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
# Simple localStorage function with Storage API fallback for chrome apps. | |
class BaseStorage | |
getItem: (key) -> | |
return sessionStorage.getItem(key) | |
setItem: (key, value) -> | |
data = {} | |
data[key] = value | |
chrome.storage.local.set data, -> | |
return sessionStorage.setItem(key, value) | |
removeItem: (key) -> | |
chrome.storage.local.remove key, -> | |
return sessionStorage.removeItem(key) | |
clear: -> | |
chrome.storage.local.clear -> | |
return sessionStorage.clear() | |
length: -> | |
return sessionStorage.length | |
key: (n) -> | |
return sessionStorage.key(n) | |
baseStorage = new BaseStorage() | |
# load data from chrome.storage to sessionStorage | |
chrome.storage.local.get null, (data) -> | |
for key, val of data | |
sessionStorage.setItem key, val | |
Object.defineProperty window, 'localStorage', { | |
get: -> | |
return baseStorage | |
set: (val) -> | |
console.log('localStorage is being set') | |
configurable: true, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment