ScriptSettings

Allows you to manage script settings through GM menu

Dit script moet niet direct worden geïnstalleerd - het is een bibliotheek voor andere scripts om op te nemen met de meta-richtlijn // @require https://update.greatest.deepsurf.us/scripts/14604/92266/ScriptSettings.js

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @id ScriptSettings
// @name ScriptSettings
// @description Allows you to manage script settings through GM menu
// @include *
// @run-at document-end
// ==/UserScript==
'use strict';
function ScriptSettings(a) {
  this.schema = a;
  this.proxy = new Proxy(this, {
      get : this.get.bind(this),
      set : this.set.bind(this),
      keys : this.keys.bind(this)
    });
  this.gui = {};
  this.createGUI();
  return this.proxy;
}
ScriptSettings.prototype.getDefault = function (a) {
  if (Array.isArray(this.schema[a])) {
    return this.schema[a][0];
  }
  switch (typeof this.schema[a]) {
  case 'number': ;
  case 'boolean': ;
  case 'string':
    return this.schema[a];
  default:
    throw Error('OH SHIT!'); ;
  }
};
ScriptSettings.prototype.get = function (a, b) {
  return GM_getValue(b, this.getDefault(b));
};
ScriptSettings.prototype.set = function (a, b, c) {
  GM_setValue(b, c);
  if (void 0 !== this.gui[b]) {
    this.gui[b].onChange();
  }
};
ScriptSettings.prototype.keys = function () {
  return GM_listValues();
};
ScriptSettings.prototype.createGUI = function () {
  for (let a in this.schema) {
    this.gui[a] = new SettingGui(this, a);
  }
};
function SettingGui(a, b) {
  let c = void 0;
  if (Array.isArray(a.schema[b])) {
    c = SettingEnum;
  } else {
    switch (typeof a.schema[b]) {
    case 'number':
      c = SettingNumber;
      break;
    case 'boolean':
      c = SettingBoolean;
      break;
    case 'string':
      c = SettingString;
      break;
    default:
      throw Error('Unsupported type: ' + typeof a.schema[b]); ;
    }
  }
  var d = Object.create(c.prototype);
  d.settings = a;
  d.name = b;
  d.act = d.act.bind(d);
  d.onChange();
  c.call(d, b);
  return d;
}
SettingGui.prototype.onChange = function () {
  this.command && GM_unregisterMenuCommand(this.command);
  this.command = GM_registerMenuCommand(this.name + ':' + this.settings.proxy[this.name], this.act);
};
SettingGui.prototype.act = function () {
  this.settings.proxy[this.name] = this.ackquire();
};
function SettingEnum(a) {
  this.val = 0;
}
SettingEnum.prototype = Object.create(SettingGui.prototype);
SettingEnum.prototype.ackquire = function () {
  this.val = (this.val + 1) % this.settings.schema[this.name].length;
  return this.settings.schema[this.name][this.val];
};
function SettingString(a) {}
SettingString.prototype = Object.create(SettingGui.prototype);
SettingString.prototype.ackquire = function () {
  return prompt('input new value of ' + this.name, this.settings.proxy[this.name]);
};
function SettingNumber(a) {}
SettingNumber.prototype = Object.create(SettingString.prototype);
SettingNumber.prototype.ackquire = function () {
  this.settings.proxy[this.name] = new Number(SettingString.prototype.ackquire.call(this));
};
function SettingBoolean(a) {}
SettingBoolean.prototype = Object.create(SettingString.prototype);
SettingBoolean.prototype.ackquire = function () {
  this.settings.proxy[this.name] = !this.settings.proxy[this.name];
};