diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-10-06 09:42:49 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-10-06 09:42:49 +0200 |
commit | ce1e0e187339976fc3cae7ef70bcfdd0eaf68981 (patch) | |
tree | c71ab2b8badd7a54759790bde083cab721796d92 /theme/javascript/storage.js | |
parent | bcb224d1cb0add4285538f03956f6f6a1574e49a (diff) | |
parent | c2775b3b63e92a7047f019a9d56851ce4fdd6c02 (diff) | |
download | gitbook-ce1e0e187339976fc3cae7ef70bcfdd0eaf68981.zip gitbook-ce1e0e187339976fc3cae7ef70bcfdd0eaf68981.tar.gz gitbook-ce1e0e187339976fc3cae7ef70bcfdd0eaf68981.tar.bz2 |
Merge pull request #964 from GitbookIO/features/plugin_api
Better APIs for Plugins
Diffstat (limited to 'theme/javascript/storage.js')
-rw-r--r-- | theme/javascript/storage.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/theme/javascript/storage.js b/theme/javascript/storage.js new file mode 100644 index 0000000..7a7643c --- /dev/null +++ b/theme/javascript/storage.js @@ -0,0 +1,37 @@ +var baseKey = ''; + +/* + * Simple module for storing data in the browser's local storage + */ +module.exports = { + setBaseKey: function(key) { + baseKey = key; + }, + + // Write something in localstorage + set: function(key, value) { + key = baseKey+':'+key; + + try { + localStorage[key] = JSON.stringify(value); + } catch(e) {} + }, + + // Read a value from localstorage + get: function(key, def) { + key = baseKey+':'+key; + if (localStorage[key] === undefined) return def; + try { + var v = JSON.parse(localStorage[key]); + return v == null ? def : v;; + } catch(err) { + return localStorage[key] || def; + } + }, + + // Remove a key from localstorage + remove: function(key) { + key = baseKey+':'+key; + localStorage.removeItem(key); + } +}; |