electron/lib/renderer/extensions/storage.js
Jessica Lord 39e6081a00 🔥 line
2016-06-10 09:59:47 -07:00

59 lines
1.2 KiB
JavaScript

const getStorage = () => {
const data = window.localStorage.getItem('__chrome.storage.sync__')
if (data != null) {
return JSON.parse(data)
} else {
return {}
}
}
const setStorage = (storage) => {
const json = JSON.stringify(storage)
window.localStorage.setItem('__chrome.storage.sync__', json)
}
module.exports = {
sync: {
get (keys, callback) {
const storage = getStorage()
if (keys == null) return storage
let defaults = {}
switch (typeof keys) {
case 'string':
keys = [keys]
break
case 'object':
if (!Array.isArray(keys)) {
defaults = keys
keys = Object.keys(keys)
}
break
}
if (keys.length === 0) return {}
let items = {}
keys.forEach(function (key) {
var value = storage[key]
if (value == null) value = defaults[key]
items[key] = value
})
setTimeout(function () {
callback(items)
})
},
set (items, callback) {
const storage = getStorage()
Object.keys(items).forEach(function (name) {
storage[name] = items[name]
})
setStorage(storage)
setTimeout(callback)
}
}
}