DataCache - Simple storage wrapper

Allows JSON data to be persisted easily by user scripts

Fra og med 15.06.2015. Se den nyeste version.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greasyfork.org/scripts/10443/56961/DataCache%20-%20Simple%20storage%20wrapper.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

function DataCache(key) {
  this._key = key;
  this._data = {};
  this.load();
}
 
DataCache.prototype = {
  getValue: function(k) {
    return this._data[k];
  },
  setValue: function(k, v) {
    this._data[k] = v;
    this.save();
  },
  deleteValue: function(k) {
    if (k in this._data) {
      delete this._data[k];
      this.save();
    }
  },
  hasValue: function(k) {
    return k in this._data;
  },
  listValues: function() {
    return Object.keys(this._data).sort();
  },
  clear: function() {
    this._data = {};
    this.save();
  },
  save: function() {
    var s = JSON.stringify(this._data);
    GM_setValue(this._key, s);
    console.info('Cache(' + this._key + ') saved: ' + s);
  },
  load: function(s) {
    try {
      this._data = JSON.parse(s || GM_getValue(this._key));
    }
    catch (ex) {
      this.clear();
    }
  },
  edit: function() {
    var res = window.prompt('Edit cached package URLs', JSON.stringify(this._data, null, 2));
    if (res !== null) {
      try {
        this._data = res ? JSON.parse(res) : {};
        this.save();
      }
      catch (ex) {
        console.warn('Failed to update cache data: %s %o', ex.toString(), ex);
      }
    }
  },
  toString: function() {
    return 'Cache(' + this._key + '): [' + this.listValues.join('\n') + ']';
  },
  dump: function() {
    console.log('Cache(' + this._key + '):\n' + JSON.stringify(this._data, null, 2));
  }
};