Add storage file

This commit is contained in:
Jessica Lord 2016-06-09 14:14:14 -07:00
parent 82bee12c00
commit 41d6ddd588
3 changed files with 61 additions and 6 deletions

View file

@ -63,6 +63,7 @@
'lib/renderer/api/remote.js',
'lib/renderer/api/screen.js',
'lib/renderer/api/web-frame.js',
'lib/renderer/extensions/storage.js'
],
'js2c_sources': [
'lib/common/asar.js',

View file

@ -183,12 +183,7 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
onMessage: chrome.runtime.onMessage
}
chrome.storage = {
sync: {
get () {},
set () {}
}
}
chrome.storage = require('./extensions/storage.js')
chrome.pageAction = {
show () {},

View file

@ -0,0 +1,59 @@
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 === {} || keys === []) return {}
if (keys === null) return storage
if (typeof keys === 'string') keys = [keys]
let items = {}
// ['foo'] or ['foo', 'bar'] or [{foo: 'bar'}]
keys.forEach(function (key) {
if (typeof key === 'string') {
items[key] = storage[key]
}
else {
const objKey = Object.keys(key)
if (!storage[objKey] || storage[objKey] === '') {
items[objKey] = key[objKey]
} else {
items[objKey] = storage[objKey]
}
}
})
setTimeout(function () {
callback(items)
})
},
set (items, callback) {
const storage = getStorage()
Object.keys(items).forEach(function (name) {
storage[name] = items[name]
})
setStorage(storage)
setTimeout(callback)
}
}
}