Config_Manager

设置获取配置的APIs

当前为 2023-12-09 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/476522/1292620/Config_Manager.js

  1. const cfg = {};
  2. (() => {
  3.  
  4. const config = {};
  5.  
  6. const inplace = (() => {
  7. 'use strict';
  8. const id = "test";
  9. try{
  10. const cap = GM_registerMenuCommand("测试", Function.prototype, {id});
  11. GM_unregisterMenuCommand(cap);
  12. return (cap === id);
  13. } catch(e) {return false;}
  14. })();
  15.  
  16. const register = (() => {
  17. 'use strict';
  18. const boolean_prefix = ["❌(已禁用) ", "✅(已启用) "];
  19. const int_family = ['uint', 'int'];
  20. return ({name, default: def, type = "other", desc: prompts, tips: ipt, input: func = prompt, callback: listener = Function.prototype, init = false, autoClose = true, judge: _jud, temp = false}) => {
  21. // let val = GM_getValue(name, def);
  22. // config[name] = val;
  23. const judge = (() => {
  24. if (typeof _jud === 'function') return _jud;
  25. return () => true;
  26. })();
  27. if (!temp) Object.defineProperty(config, name, {
  28. get: () => GM_getValue(name, def),
  29. set: val => GM_setValue(name, val)
  30. });
  31. if (typeof init === 'function') init(name, config[name]);
  32. if (int_family.includes(type)) {
  33. const judge = (type === 'uint') ? (s => (s|0) < 0) : (()=>false);
  34. if (func === prompt) {
  35. func = () => {
  36. let p;
  37. do {p = prompt(ipt, config[name]);} while(isNaN(p) || judge(p));
  38. return p | 0;
  39. };
  40. }
  41. type = 'other';
  42. }
  43. const cfg = {id: name, autoClose};
  44. if (type === 'bool') {
  45. let cont;
  46. const reg = () => cont = GM_registerMenuCommand(boolean_prefix[config[name] | 0] + prompts, () => {
  47. const newval = !config[name];
  48. if (judge(name, newval))
  49. config[name] = newval;
  50. }, cfg);
  51. if (true == init) listener(name, config[name], config[name], cont);
  52. const stn = (() => {
  53. const re_reg = inplace ? reg : () => (GM_unregisterMenuCommand(cont), reg());
  54. return (_1, ov, nv) => (reg(), listener(name, ov, nv, cont));
  55. })();
  56. GM_addValueChangeListener(name, stn);
  57. reg();
  58. } else if (type === "other") {
  59. if (func === prompt) func = () => prompt(ipt, config[name]);
  60. const cont = GM_registerMenuCommand(prompts, () => {
  61. const inp = func();
  62. if (judge(name, inp))
  63. GM_setValue(name, inp);
  64. }, cfg);
  65. if (true == init) listener(name, config[name], config[name], cont);
  66. GM_addValueChangeListener(name, (_1, ov, nv) => listener(name, ov, nv, cont));
  67. }
  68. console.debug(name, "in type", type, "\nRegistered!");
  69. };
  70. })();
  71.  
  72. cfg.config = config;
  73. cfg.register = register;
  74. })();