Config_Manager

设置获取配置的APIs

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/476522/1292633/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. else tmpu[name] = def;
  33. if (typeof init === 'function') init(name, config[name]);
  34. if (int_family.includes(type)) {
  35. const judge = (type === 'uint') ? (s => (s|0) < 0) : (()=>false);
  36. if (func === prompt) {
  37. func = () => {
  38. let p;
  39. do {p = prompt(ipt, config[name]);} while(isNaN(p) || judge(p));
  40. return p | 0;
  41. };
  42. }
  43. type = 'other';
  44. }
  45. const cfg = {id: name, autoClose};
  46. if (type === 'bool') {
  47. let cont;
  48. const reg = () => cont = GM_registerMenuCommand(boolean_prefix[config[name] | 0] + prompts, () => {
  49. const newval = !config[name];
  50. if (judge(name, newval))
  51. config[name] = newval;
  52. }, cfg);
  53. if (true == init) listener(name, config[name], config[name], cont);
  54. const stn = (() => {
  55. const re_reg = inplace ? reg : () => (GM_unregisterMenuCommand(cont), reg());
  56. return (_1, ov, nv) => (reg(), listener(name, ov, nv, cont));
  57. })();
  58. if (!temp) GM_addValueChangeListener(name, stn);
  59. else Object.defineProperty(config, name, {
  60. get: () => tmpu[name],
  61. set: v => (stn(name, tmpu[name], v), tmpu[name] = v),
  62. });
  63. reg();
  64. } else if (type === "other") {
  65. if (func === prompt) func = () => prompt(ipt, config[name]);
  66. const cont = GM_registerMenuCommand(prompts, () => {
  67. const inp = func();
  68. if (judge(name, inp))
  69. GM_setValue(name, inp);
  70. }, cfg);
  71. if (true == init) listener(name, config[name], config[name], cont);
  72. if (!temp) GM_addValueChangeListener(name, (_1, ov, nv) => listener(name, ov, nv, cont));
  73. else Object.defineProperty(config, name, {
  74. get: () => tmpu[name],
  75. set: v => (listener(name, tmpu[name], v, cont), tmpu[name] = v),
  76. });
  77. }
  78. console.debug(name, "in type", type, "\nRegistered!");
  79. };
  80. })();
  81.  
  82. cfg.config = config;
  83. cfg.register = register;
  84. })();

QingJ © 2025

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