Per-extension storage

This commit is contained in:
Alexandre Lachèze 2017-07-07 03:13:19 +02:00
parent d2002ff3fc
commit ec10338364
2 changed files with 22 additions and 20 deletions

View file

@ -173,7 +173,7 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
onMessage: chrome.runtime.onMessage onMessage: chrome.runtime.onMessage
} }
chrome.storage = require('./extensions/storage') chrome.storage = require('./extensions/storage').setup(extensionId)
chrome.pageAction = { chrome.pageAction = {
show () {}, show () {},

View file

@ -3,18 +3,18 @@ const path = require('path')
const { remote } = require('electron') const { remote } = require('electron')
const { app } = remote; const { app } = remote;
const getChromeStoragePath = (storageType) => { const getChromeStoragePath = (storageType, extensionId) => {
return path.join( return path.join(
app.getPath('userData'), `/Chrome Storage/${storageType}.json`) app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
} }
const readChromeStorageFile = (storageType) => { const readChromeStorageFile = (storageType, extensionId) => {
const filePath = getChromeStoragePath(storageType) const filePath = getChromeStoragePath(storageType, extensionId)
if(!fs.existsSync(filePath)) return null if(!fs.existsSync(filePath)) return null
return fs.readFileSync(filePath, 'utf8') return fs.readFileSync(filePath, 'utf8')
} }
const writeChromeStorageFile = (storageType, data) => { const writeChromeStorageFile = (storageType, extensionId, data) => {
const filePath = getChromeStoragePath(storageType) const filePath = getChromeStoragePath(storageType, extensionId)
try { try {
fs.mkdirSync(path.dirname(filePath)) fs.mkdirSync(path.dirname(filePath))
} catch (error) { } catch (error) {
@ -23,8 +23,8 @@ const writeChromeStorageFile = (storageType, data) => {
return fs.writeFileSync(filePath, data) return fs.writeFileSync(filePath, data)
} }
const getStorage = (storageType) => { const getStorage = (storageType, extensionId) => {
const data = readChromeStorageFile(storageType) const data = readChromeStorageFile(storageType, extensionId)
if (data != null) { if (data != null) {
return JSON.parse(data) return JSON.parse(data)
} else { } else {
@ -32,9 +32,9 @@ const getStorage = (storageType) => {
} }
} }
const setStorage = (storageType, storage) => { const setStorage = (storageType, extensionId, storage) => {
const json = JSON.stringify(storage) const json = JSON.stringify(storage)
const data = writeChromeStorageFile(storageType, json) const data = writeChromeStorageFile(storageType, extensionId, json)
} }
const scheduleCallback = (items, callback) => { const scheduleCallback = (items, callback) => {
@ -43,10 +43,10 @@ const scheduleCallback = (items, callback) => {
}) })
} }
const getStorageManager = (storageType) => { const getStorageManager = (storageType, extensionId) => {
return { return {
get (keys, callback) { get (keys, callback) {
const storage = getStorage(storageType) const storage = getStorage(storageType, extensionId)
if (keys == null) return scheduleCallback(storage, callback) if (keys == null) return scheduleCallback(storage, callback)
let defaults = {} let defaults = {}
@ -73,19 +73,19 @@ const getStorageManager = (storageType) => {
}, },
set (items, callback) { set (items, callback) {
const storage = getStorage(storageType) const storage = getStorage(storageType, extensionId)
Object.keys(items).forEach(function (name) { Object.keys(items).forEach(function (name) {
storage[name] = items[name] storage[name] = items[name]
}) })
setStorage(storageType, storage) setStorage(storageType, extensionId, storage)
setTimeout(callback) setTimeout(callback)
}, },
remove (keys, callback) { remove (keys, callback) {
const storage = getStorage(storageType) const storage = getStorage(storageType, extensionId)
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) {
keys = [keys] keys = [keys]
@ -94,13 +94,13 @@ const getStorageManager = (storageType) => {
delete storage[key] delete storage[key]
}) })
setStorage(storageType, storage) setStorage(storageType, extensionId, storage)
setTimeout(callback) setTimeout(callback)
}, },
clear (callback) { clear (callback) {
setStorage(storageType, {}) setStorage(storageType, extensionId, {})
setTimeout(callback) setTimeout(callback)
} }
@ -108,6 +108,8 @@ const getStorageManager = (storageType) => {
} }
module.exports = { module.exports = {
sync: getStorageManager('sync'), setup: extensionId => ({
local: getStorageManager('local') sync: getStorageManager('sync', extensionId),
local: getStorageManager('local', extensionId)
})
} }