commit
6081cba15d
1 changed files with 14 additions and 9 deletions
|
@ -1,5 +1,5 @@
|
||||||
const getStorage = () => {
|
const getStorage = (storageType) => {
|
||||||
const data = window.localStorage.getItem('__chrome.storage.sync__')
|
const data = window.localStorage.getItem(`__chrome.storage.${storageType}__`)
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
return JSON.parse(data)
|
return JSON.parse(data)
|
||||||
} else {
|
} else {
|
||||||
|
@ -7,15 +7,15 @@ const getStorage = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const setStorage = (storage) => {
|
const setStorage = (storageType, storage) => {
|
||||||
const json = JSON.stringify(storage)
|
const json = JSON.stringify(storage)
|
||||||
window.localStorage.setItem('__chrome.storage.sync__', json)
|
window.localStorage.setItem(`__chrome.storage.${storageType}__`, json)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
const getStorageManager = (storageType) => {
|
||||||
sync: {
|
return {
|
||||||
get (keys, callback) {
|
get (keys, callback) {
|
||||||
const storage = getStorage()
|
const storage = getStorage(storageType)
|
||||||
if (keys == null) return storage
|
if (keys == null) return storage
|
||||||
|
|
||||||
let defaults = {}
|
let defaults = {}
|
||||||
|
@ -45,15 +45,20 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
set (items, callback) {
|
set (items, callback) {
|
||||||
const storage = getStorage()
|
const storage = getStorage(storageType)
|
||||||
|
|
||||||
Object.keys(items).forEach(function (name) {
|
Object.keys(items).forEach(function (name) {
|
||||||
storage[name] = items[name]
|
storage[name] = items[name]
|
||||||
})
|
})
|
||||||
|
|
||||||
setStorage(storage)
|
setStorage(storageType, storage)
|
||||||
|
|
||||||
setTimeout(callback)
|
setTimeout(callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
sync: getStorageManager('sync'),
|
||||||
|
local: getStorageManager('local')
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue