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
}
chrome.storage = require('./extensions/storage')
chrome.storage = require('./extensions/storage').setup(extensionId)
chrome.pageAction = {
show () {},

View file

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