From d6ab3534387e4be2cea0309663f2586941f2f61c Mon Sep 17 00:00:00 2001 From: Ian Ornelas Date: Tue, 28 Jun 2016 06:02:25 -0300 Subject: [PATCH] Add chrome.storage.local --- lib/renderer/extensions/storage.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/renderer/extensions/storage.js b/lib/renderer/extensions/storage.js index 7b880f08310e..b0941d98c8ce 100644 --- a/lib/renderer/extensions/storage.js +++ b/lib/renderer/extensions/storage.js @@ -1,5 +1,5 @@ -const getStorage = () => { - const data = window.localStorage.getItem('__chrome.storage.sync__') +const getStorage = (storageType) => { + const data = window.localStorage.getItem(`__chrome.storage.${storageType}__`) if (data != null) { return JSON.parse(data) } else { @@ -7,15 +7,15 @@ const getStorage = () => { } } -const setStorage = (storage) => { +const setStorage = (storageType, storage) => { const json = JSON.stringify(storage) - window.localStorage.setItem('__chrome.storage.sync__', json) + window.localStorage.setItem(`__chrome.storage.${storageType}__`, json) } -module.exports = { - sync: { +const getStorageManager = (storageType) => { + return { get (keys, callback) { - const storage = getStorage() + const storage = getStorage(storageType) if (keys == null) return storage let defaults = {} @@ -45,15 +45,20 @@ module.exports = { }, set (items, callback) { - const storage = getStorage() + const storage = getStorage(storageType) Object.keys(items).forEach(function (name) { storage[name] = items[name] }) - setStorage(storage) + setStorage(storageType, storage) setTimeout(callback) } } } + +module.exports = { + sync: getStorageManager('sync'), + local: getStorageManager('local') +}