Config_Manager

设置获取配置的APIs

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

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

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址