UNPKG

chrome-cross-storage

Version:

Google chrome storage and localStorage wrapper which help users debug google chrome application which depends on the storage as simple web apps

78 lines (77 loc) 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Storage { constructor(type) { this.GOOGLE_CHROME_STORAGE = 'GOOGLE_CHROME_STORAGE'; this.LOCAL_STORAGE = 'LOCAL_STORAGE'; this.storageMethod = type === 'chrome' ? this.GOOGLE_CHROME_STORAGE : this.LOCAL_STORAGE; } set(key, value, callback = () => { }) { if (this.storageMethod === this.GOOGLE_CHROME_STORAGE) { chrome.storage.sync.set({ [key]: value, }, callback); } else { localStorage.setItem(key, value); callback(); } } get(keys, callback = arg => { }) { if (this.storageMethod === this.GOOGLE_CHROME_STORAGE) { chrome.storage.sync.get(keys, callback); } else { if (typeof keys === 'string') { const result = localStorage.getItem(keys); callback({ [keys]: result }); } else { const result = keys && keys.reduce((acc, next) => { return Object.assign({}, acc, { [next]: localStorage.getItem(next) }); }, {}); callback(result); } } } getAll(callback = arg => { }) { if (this.storageMethod === this.GOOGLE_CHROME_STORAGE) { this.get(null, callback); } else { const values = {}; const keys = Object.keys(localStorage); let i = keys.length; while (i--) { values[keys[i]] = localStorage.getItem(keys[i]); } callback(values); } } remove(keys, callback = () => { }) { if (this.storageMethod === this.GOOGLE_CHROME_STORAGE) { chrome.storage.sync.remove(keys, callback); } else { if (typeof keys === 'string') { localStorage.removeItem(keys); } else { keys.map(key => localStorage.removeItem(key)); } callback(); } } clear(callback = () => { }) { if (this.storageMethod === this.GOOGLE_CHROME_STORAGE) { chrome.storage.sync.clear(callback); } else { localStorage.clear(); callback(); } } } exports.default = Storage;