Config_Manager

设置获取配置的APIs

当前为 2023-10-05 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/476522/1259949/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. // @description 设置获取配置的APIs
  11. // ==/UserScript==
  12. 'use strict';
  13.  
  14. let config = {};
  15. let register;
  16.  
  17. (() => {
  18. // const _APIs = ["GM_getValue", "GM_setValue", "GM_addValueChangeListener", "GM_registerMenuCommand", "GM_unregisterMenuCommand"];
  19. // for (const val in _APIs)
  20. // if (!(val in globalThis)) throw new Error("需要API权限!!!");
  21. const boolean_prefix = ["❌(已禁用)", "✅(已启用)"];
  22. const int_family = ['uint', 'int'];
  23. config = {};
  24. register = ({name, default: def, type = "other", prompts, tip: ipt, input: func = prompt, callback: listener = Function.prototype}) => {
  25. let val = GM_getValue(name, def);
  26. config[name] = val;
  27. if (int_family.includes(type)) {
  28. const judge = (type === 'uint') ? (s => (s|0) < 0) : (()=>false);
  29. if (input === prompt) {
  30. input = () => {
  31. let p;
  32. do {p = prompt(ipt);} while(isNaN(p) || judge(p));
  33. return p;
  34. };
  35. }
  36. type = 'other';
  37. }
  38. if (type === 'bool') {
  39. let cont;
  40. const reg = () => cont = GM_registerMenuCommand(boolean_prefix[val | 0] + prompts, () => GM_setValue(name, !val));
  41. GM_addValueChangeListener(name, (_1, ov, newval) => {
  42. config[name] = val = newval;
  43. GM_unregisterMenuCommand(cont);
  44. reg();
  45. listener(name, ov, newval, cont);
  46. });
  47. reg();
  48. } else if (type === "other") {
  49. const cont = GM_registerMenuCommand(prompts, () => GM_setValue(name, func()));
  50. GM_addValueChangeListener(name, (_1, ov, newval) => (config[name] = newval, listener(name, ov, newval, cont)));
  51. }
  52. console.debug(type, name, "\nRegistered!");
  53. };
  54. })();

QingJ © 2025

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