Greasy Fork 支持简体中文。

Config_Manager

设置获取配置的APIs

目前為 2023-10-22 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/476522/1268471/Config_Manager.js

  1. 'use strict';
  2.  
  3. (() => {
  4. 'use strict';
  5. const _APIs = ["GM_getValue", "GM_setValue", "GM_addValueChangeListener", "GM_registerMenuCommand", "GM_unregisterMenuCommand"];
  6. for (const val in _APIs)
  7. if (eval("typeof " + val) === 'undefined') throw new Error("需要" + val + " API权限!!!");
  8. })();
  9.  
  10. const config = {};
  11.  
  12. const inplace = (() => {
  13. const id = "test";
  14. const cap = GM_registerMenuCommand("测试", Function.prototype, {id});
  15. GM_unregisterMenuCommand(cap);
  16. return (cap === id);
  17. })();
  18.  
  19. const register = (() => {
  20. const boolean_prefix = ["❌(已禁用) ", "✅(已启用) "];
  21. const int_family = ['uint', 'int'];
  22. return ({name, default: def, type = "other", desc: prompts, tips: ipt, input: func = prompt, callback: listener = Function.prototype, init = false, autoClose = true, judge: _jud}) => {
  23. // let val = GM_getValue(name, def);
  24. // config[name] = val;
  25. const judge = (() => {
  26. if (typeof _jud === 'function') return _jud;
  27. return () => true;
  28. })();
  29. Object.defineProperty(config, name, {
  30. get: () => GM_getValue(name, def),
  31. set: val => GM_setValue(name, val)
  32. });
  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);} 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. GM_addValueChangeListener(name, stn);
  59. reg();
  60. } else if (type === "other") {
  61. if (func === prompt) func = () => prompt(ipt);
  62. const cont = GM_registerMenuCommand(prompts, () => {
  63. const inp = func();
  64. if (judge(name, inp))
  65. GM_setValue(name, inp);
  66. }, cfg);
  67. if (true == init) listener(name, config[name], config[name], cont);
  68. GM_addValueChangeListener(name, (_1, ov, nv) => listener(name, ov, nv, cont));
  69. }
  70. console.debug(type, name, "\nRegistered!");
  71. };
  72. })();