add variable item name in localstore
This commit is contained in:
parent
ce1239f4e8
commit
0f16fc4be4
|
@ -19,25 +19,31 @@
|
||||||
* The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
|
* The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
|
||||||
* @requires openpgp
|
* @requires openpgp
|
||||||
* @module keyring/localstore
|
* @module keyring/localstore
|
||||||
|
* @param {String} item itemname in localstore
|
||||||
*/
|
*/
|
||||||
module.exports = LocalStore;
|
module.exports = LocalStore;
|
||||||
|
|
||||||
var openpgp = require('../');
|
var openpgp = require('../');
|
||||||
|
|
||||||
function LocalStore() {
|
function LocalStore(item) {
|
||||||
if (typeof window != 'undefined' && window.localStorage) {
|
if (typeof window != 'undefined' && window.localStorage) {
|
||||||
this.storage = window.localStorage;
|
this.storage = window.localStorage;
|
||||||
} else {
|
} else {
|
||||||
this.storage = new (require('node-localstorage').LocalStorage)(openpgp.config.node_store);
|
this.storage = new (require('node-localstorage').LocalStorage)(openpgp.config.node_store);
|
||||||
}
|
}
|
||||||
|
if(typeof item == 'string') {
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LocalStore.item = 'armoredKeys';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the keyring from HTML5 local storage and initializes this instance.
|
* Load the keyring from HTML5 local storage and initializes this instance.
|
||||||
* @return {Array<module:key~Key>} array of keys retrieved from localstore
|
* @return {Array<module:key~Key>} array of keys retrieved from localstore
|
||||||
*/
|
*/
|
||||||
LocalStore.prototype.load = function () {
|
LocalStore.prototype.load = function () {
|
||||||
var armoredKeys = JSON.parse(this.storage.getItem('armoredKeys'));
|
var armoredKeys = JSON.parse(this.storage.getItem(this.item));
|
||||||
var keys = [];
|
var keys = [];
|
||||||
if (armoredKeys !== null && armoredKeys.length !== 0) {
|
if (armoredKeys !== null && armoredKeys.length !== 0) {
|
||||||
var key;
|
var key;
|
||||||
|
@ -59,5 +65,5 @@ LocalStore.prototype.store = function (keys) {
|
||||||
for (var i = 0; i < keys.length; i++) {
|
for (var i = 0; i < keys.length; i++) {
|
||||||
armoredKeys.push(keys[i].armor());
|
armoredKeys.push(keys[i].armor());
|
||||||
}
|
}
|
||||||
this.storage.setItem('armoredKeys', JSON.stringify(armoredKeys));
|
this.storage.setItem(this.item, JSON.stringify(armoredKeys));
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user