gm_config_toolbar

greasyfork configuration toolbar on the script addins

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greatest.deepsurf.us/scripts/389774/921639/gm_config_toolbar.js

  1. "use strict";
  2.  
  3. // ==UserScript==
  4. // @name gm_config_toolbar
  5. // @version 2020.4.20
  6. // @namespace https://github.com/niubilityfrontend
  7. // @description greasyfork configuration toolbar on the script addins
  8. // @author kufii
  9. // @license OSL-3.0
  10. // @match *
  11. // @include *
  12. // @supportURL https://github.com/kufii/My-UserScripts
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_listValues
  17. // @grant GM_deleteValue
  18. // @grant GM_registerMenuCommand
  19. // ==/UserScript==
  20. (function () {
  21. 'use strict';
  22.  
  23. window.GM_config = function (settings) {
  24. var storage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cfg',
  25. ret = null,
  26. prefix = 'gm-config',
  27. addStyle = function addStyle() {
  28. var css = "\n\t\t\t\t.".concat(prefix, " {\n\t\t\t\t\tdisplay: grid;\n\t\t\t\t\talign-items: center;\n\t\t\t\t\tgrid-row-gap: 5px;\n\t\t\t\t\tgrid-column-gap: 10px;\n\t\t\t\t\tbackground-color: white;\n\t\t\t\t\tborder: 1px solid black;\n\t\t\t\t\tpadding: 5px;\n\t\t\t\t\tposition: fixed;\n\t\t\t\t\ttop: 0;\n\t\t\t\t\tright: 0;\n\t\t\t\t\tz-index: 2147483647;\n\t\t\t\t}\n\n\t\t\t\t.").concat(prefix, " label {\n\t\t\t\t\tgrid-column: 1 / 2;\n\t\t\t\t\tcolor: black;\n\t\t\t\t\ttext-align: right;\n\t\t\t\t\tfont-size: small;\n\t\t\t\t\tfont-weight: bold;\n\t\t\t\t}\n\n\t\t\t\t.").concat(prefix, " input,\n\t\t\t\t.").concat(prefix, " textarea,\n\t\t\t\t.").concat(prefix, " select {\n\t\t\t\t\tgrid-column: 2 / 4;\n\t\t\t\t}\n\n\t\t\t\t.").concat(prefix, " .").concat(prefix, "-save {\n\t\t\t\t\tgrid-column: 2 / 3;\n\t\t\t\t}\n\n\t\t\t\t.").concat(prefix, " .").concat(prefix, "-cancel {\n\t\t\t\t\tgrid-column: 3 / 4;\n\t\t\t\t}\n\t\t\t");
  29.  
  30. if (typeof GM_addStyle === 'undefined') {
  31. var style = document.createElement('style');
  32. style.textContent = css;
  33. document.head.appendChild(style);
  34. } else {
  35. GM_addStyle(css);
  36. }
  37. },
  38. load = function load() {
  39. var defaults = {};
  40. settings.forEach(function (_ref) {
  41. var {
  42. key: key,
  43. "default": def
  44. } = _ref;
  45. return defaults[key] = def;
  46. });
  47. var cfg = typeof GM_getValue !== 'undefined' ? GM_getValue(storage) : localStorage.getItem(storage);
  48. if (!cfg) return defaults;
  49. cfg = JSON.parse(cfg);
  50. Object.entries(defaults).forEach(function (_ref2) {
  51. var [key, value] = _ref2;
  52.  
  53. if (typeof cfg[key] === 'undefined') {
  54. cfg[key] = value;
  55. }
  56. });
  57. return cfg;
  58. },
  59. save = function save(cfg) {
  60. var data = JSON.stringify(cfg);
  61. typeof GM_setValue !== 'undefined' ? GM_setValue(storage, data) : localStorage.setItem(storage, data);
  62. },
  63. setup = function setup() {
  64. var createContainer = function createContainer() {
  65. var form = document.createElement('form');
  66. form.classList.add(prefix);
  67. return form;
  68. },
  69. createTextbox = function createTextbox(name, value, placeholder, maxLength, multiline, resize) {
  70. var input = document.createElement(multiline ? 'textarea' : 'input');
  71.  
  72. if (multiline) {
  73. input.style.resize = resize ? 'vertical' : 'none';
  74. } else {
  75. input.type = 'text';
  76. }
  77.  
  78. input.name = name;
  79. if (typeof value !== 'undefined') input.value = value;
  80. if (placeholder) input.placeholder = placeholder;
  81. if (maxLength) input.maxLength = maxLength;
  82. return input;
  83. },
  84. createNumber = function createNumber(name, value, placeholder, min, max, step) {
  85. var input = createTextbox(name, value, placeholder);
  86. input.type = 'number';
  87. if (typeof min !== 'undefined') input.min = min;
  88. if (typeof max !== 'undefined') input.max = max;
  89. if (typeof step !== 'undefined') input.step = step;
  90. return input;
  91. },
  92. createSelect = function createSelect(name, options, value, showBlank) {
  93. var select = document.createElement('select');
  94. select.name = name;
  95.  
  96. var createOption = function createOption(val) {
  97. var {
  98. value = val,
  99. text = val
  100. } = val,
  101. option = document.createElement('option');
  102. option.value = value;
  103. option.textContent = text;
  104. return option;
  105. };
  106.  
  107. if (showBlank) {
  108. select.appendChild(createOption(''));
  109. }
  110.  
  111. options.forEach(function (opt) {
  112. if (typeof opt.optgroup !== 'undefined') {
  113. var optgroup = document.createElement('optgroup');
  114. optgroup.label = opt.optgroup;
  115. select.appendChild(optgroup);
  116. opt.values.forEach(function (value) {
  117. return optgroup.appendChild(createOption(value));
  118. });
  119. } else {
  120. select.appendChild(createOption(opt));
  121. }
  122. });
  123. select.value = value;
  124. return select;
  125. },
  126. createCheckbox = function createCheckbox(name, checked) {
  127. var checkbox = document.createElement('input');
  128. checkbox.id = "".concat(prefix, "-").concat(name);
  129. checkbox.type = 'checkbox';
  130. checkbox.name = name;
  131. checkbox.checked = checked;
  132. return checkbox;
  133. },
  134. createButton = function createButton(text, onclick, classname) {
  135. var button = document.createElement('button');
  136. button.classList.add("".concat(prefix, "-").concat(classname));
  137. button.textContent = text;
  138. button.onclick = onclick;
  139. return button;
  140. },
  141. createLabel = function createLabel(label, htmlFor) {
  142. var lbl = document.createElement('label');
  143. if (htmlFor) lbl.htmlFor = htmlFor;
  144. lbl.textContent = label;
  145. return lbl;
  146. },
  147. init = function init(cfg) {
  148. var controls = {},
  149. div = createContainer();
  150. settings.filter(function (_ref3) {
  151. var {
  152. type: type
  153. } = _ref3;
  154. return type !== 'hidden';
  155. }).forEach(function (setting) {
  156. var value = cfg[setting.key],
  157. control;
  158.  
  159. if (setting.type === 'text') {
  160. control = createTextbox(setting.key, value, setting.placeholder, setting.maxLength, setting.multiline, setting.resizable);
  161. } else if (setting.type === 'number') {
  162. control = createNumber(setting.key, value, setting.placeholder, setting.min, setting.max, setting.step);
  163. } else if (setting.type === 'dropdown') {
  164. control = createSelect(setting.key, setting.values, value, setting.showBlank);
  165. } else if (setting.type === 'bool') {
  166. control = createCheckbox(setting.key, value);
  167. }
  168.  
  169. div.appendChild(createLabel(setting.label, control.id));
  170. div.appendChild(control);
  171. controls[setting.key] = control;
  172. control.addEventListener(setting.type === 'dropdown' ? 'change' : 'input', function () {
  173. if (ret.onchange) {
  174. var control = controls[setting.key],
  175. _value = setting.type === 'bool' ? control.checked : control.value;
  176.  
  177. ret.onchange(setting.key, _value);
  178. }
  179. });
  180. });
  181. div.appendChild(createButton('Save', function () {
  182. settings.filter(function (_ref4) {
  183. var {
  184. type: type
  185. } = _ref4;
  186. return type !== 'hidden';
  187. }).forEach(function (_ref5) {
  188. var {
  189. key: key,
  190. type: type
  191. } = _ref5,
  192. control = controls[key];
  193. cfg[key] = type === 'bool' ? control.checked : control.value;
  194. });
  195. save(cfg);
  196.  
  197. if (ret.onsave) {
  198. ret.onsave(cfg);
  199. }
  200.  
  201. div.remove();
  202. }, 'save'));
  203. div.appendChild(createButton('Cancel', function () {
  204. if (ret.oncancel) {
  205. ret.oncancel(cfg);
  206. }
  207.  
  208. div.remove();
  209. }, 'cancel'));
  210. document.body.appendChild(div);
  211. };
  212.  
  213. init(load());
  214. };
  215.  
  216. addStyle();
  217. ret = {
  218. load: load,
  219. save: save,
  220. setup: setup
  221. };
  222. return ret;
  223. };
  224. })();