Config_Manager

设置获取配置的APIs

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

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

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