ScriptSettings

Allows you to manage script settings through GM menu

Dette scriptet burde ikke installeres direkte. Det er et bibliotek for andre script å inkludere med det nye metadirektivet // @require https://update.greatest.deepsurf.us/scripts/14604/92266/ScriptSettings.js

  1. // ==UserScript==
  2. // @id ScriptSettings
  3. // @name ScriptSettings
  4. // @description Allows you to manage script settings through GM menu
  5. // @include *
  6. // @run-at document-end
  7. // ==/UserScript==
  8. 'use strict';
  9. function ScriptSettings(a) {
  10. this.schema = a;
  11. this.proxy = new Proxy(this, {
  12. get : this.get.bind(this),
  13. set : this.set.bind(this),
  14. keys : this.keys.bind(this)
  15. });
  16. this.gui = {};
  17. this.createGUI();
  18. return this.proxy;
  19. }
  20. ScriptSettings.prototype.getDefault = function (a) {
  21. if (Array.isArray(this.schema[a])) {
  22. return this.schema[a][0];
  23. }
  24. switch (typeof this.schema[a]) {
  25. case 'number': ;
  26. case 'boolean': ;
  27. case 'string':
  28. return this.schema[a];
  29. default:
  30. throw Error('OH SHIT!'); ;
  31. }
  32. };
  33. ScriptSettings.prototype.get = function (a, b) {
  34. return GM_getValue(b, this.getDefault(b));
  35. };
  36. ScriptSettings.prototype.set = function (a, b, c) {
  37. GM_setValue(b, c);
  38. if (void 0 !== this.gui[b]) {
  39. this.gui[b].onChange();
  40. }
  41. };
  42. ScriptSettings.prototype.keys = function () {
  43. return GM_listValues();
  44. };
  45. ScriptSettings.prototype.createGUI = function () {
  46. for (let a in this.schema) {
  47. this.gui[a] = new SettingGui(this, a);
  48. }
  49. };
  50. function SettingGui(a, b) {
  51. let c = void 0;
  52. if (Array.isArray(a.schema[b])) {
  53. c = SettingEnum;
  54. } else {
  55. switch (typeof a.schema[b]) {
  56. case 'number':
  57. c = SettingNumber;
  58. break;
  59. case 'boolean':
  60. c = SettingBoolean;
  61. break;
  62. case 'string':
  63. c = SettingString;
  64. break;
  65. default:
  66. throw Error('Unsupported type: ' + typeof a.schema[b]); ;
  67. }
  68. }
  69. var d = Object.create(c.prototype);
  70. d.settings = a;
  71. d.name = b;
  72. d.act = d.act.bind(d);
  73. d.onChange();
  74. c.call(d, b);
  75. return d;
  76. }
  77. SettingGui.prototype.onChange = function () {
  78. this.command && GM_unregisterMenuCommand(this.command);
  79. this.command = GM_registerMenuCommand(this.name + ':' + this.settings.proxy[this.name], this.act);
  80. };
  81. SettingGui.prototype.act = function () {
  82. this.settings.proxy[this.name] = this.ackquire();
  83. };
  84. function SettingEnum(a) {
  85. this.val = 0;
  86. }
  87. SettingEnum.prototype = Object.create(SettingGui.prototype);
  88. SettingEnum.prototype.ackquire = function () {
  89. this.val = (this.val + 1) % this.settings.schema[this.name].length;
  90. return this.settings.schema[this.name][this.val];
  91. };
  92. function SettingString(a) {}
  93. SettingString.prototype = Object.create(SettingGui.prototype);
  94. SettingString.prototype.ackquire = function () {
  95. return prompt('input new value of ' + this.name, this.settings.proxy[this.name]);
  96. };
  97. function SettingNumber(a) {}
  98. SettingNumber.prototype = Object.create(SettingString.prototype);
  99. SettingNumber.prototype.ackquire = function () {
  100. this.settings.proxy[this.name] = new Number(SettingString.prototype.ackquire.call(this));
  101. };
  102. function SettingBoolean(a) {}
  103. SettingBoolean.prototype = Object.create(SettingString.prototype);
  104. SettingBoolean.prototype.ackquire = function () {
  105. this.settings.proxy[this.name] = !this.settings.proxy[this.name];
  106. };