Greasy Fork 还支持 简体中文。

setupToggleCommands

Library that creates toggleable menu commands for userscript managers

目前為 2024-06-17 提交的版本,檢視 最新版本

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

  1. // ==UserScript==
  2. // @name setupToggleCommands
  3. // @license MIT
  4. // @namespace rtonne
  5. // @match *://*/*
  6. // @version 1.0
  7. // @author Rtonne
  8. // @description Library that creates toggleable menu commands for userscript managers
  9. // @grant GM.registerMenuCommand
  10. // @grant GM.unregisterMenuCommand
  11. // @grant GM.getValue
  12. // @grant GM.setValue
  13. // ==/UserScript==
  14.  
  15. /**
  16. * Can be the only function of this library used externally.
  17. * @param {_Command[]} command_list The list of all toggle commands that are to be added.
  18. */
  19. async function setupToggleCommands(command_list) {
  20. for (const command of command_list) {
  21. _runCommandFunction(command);
  22. }
  23. for (const command of command_list) {
  24. await _registerCommand(command_list, command);
  25. }
  26. }
  27.  
  28. /**
  29. * @typedef {Object} _Command
  30. * @property {string} id The id of the menu command and the key for the value.
  31. * @property {string} off_text The text displayed when the last function ran was toggleOffFunction.
  32. * @property {string} on_text The text displayed when the last function ran was toggleOnFunction.
  33. * @property {string} [off_tooltip] The tooltip shown while hovering the command when the last function ran was toggleOffFunction.
  34. * @property {string} [on_tooltip] The tooltip shown while hovering the command when the last function ran was toggleOnFunction.
  35. * @property {boolean} [default_value] The default value. Its "false" by default.
  36. * @property {boolean} [auto_close] If the userscript manager popup closes when the command is clicked. Its "false" by default.
  37. * @property {() => void} [toggleOffFunction] A function to be run then toggling off the command.
  38. * @property {() => void} [toggleOnFunction] A function to be run then toggling on the command.
  39. */
  40.  
  41. // To check if in place command replacement is supported
  42. // https://violentmonkey.github.io/api/gm/#gm_registermenucommand
  43. const _can_replace_in_place =
  44. "test" === GM.registerMenuCommand("test", () => {}, { id: "test" });
  45. GM.unregisterMenuCommand("test");
  46.  
  47. /**
  48. * Runs the correct command function, and toggles the command text.
  49. * @param {_Command[]} command_list The list of all commands (may be used to replace old commands).
  50. * @param {_Command} command The command being toggled.
  51. */
  52. async function _toggleCommand(command_list, command) {
  53. await GM.setValue(
  54. command.id,
  55. !(await GM.getValue(command.id, command.default_value))
  56. );
  57. _runCommandFunction(command);
  58. if (_can_replace_in_place) {
  59. await _registerCommand(command_list, command);
  60. } else {
  61. // If we can't replace commands, we need to remove them all, then re-add them
  62. for (const command of command_list) {
  63. GM.unregisterMenuCommand(command.id);
  64. }
  65. for (const command of command_list) {
  66. await _registerCommand(command_list, command);
  67. }
  68. }
  69. }
  70.  
  71. /**
  72. * Registers the command with the correct text.
  73. * @param {_Command[]} command_list The list of all commands (may be used to replace old commands).
  74. * @param {_Command} command
  75. */
  76. async function _registerCommand(command_list, command) {
  77. let text = command.on_text;
  78. let tooltip = command.on_tooltip;
  79. if (!(await GM.getValue(command.id, command.default_value))) {
  80. text = command.off_text;
  81. tooltip = command.off_tooltip;
  82. }
  83.  
  84. GM.registerMenuCommand(text, () => _toggleCommand(command_list, command), {
  85. id: command.id,
  86. title: tooltip,
  87. autoClose: command.auto_close !== undefined && command.auto_close,
  88. });
  89. }
  90.  
  91. /**
  92. * Runs the correct command function.
  93. * @param {_Command} command
  94. */
  95. async function _runCommandFunction(command) {
  96. if (await GM.getValue(command.id, command.default_value)) {
  97. if (command.toggleOnFunction) {
  98. command.toggleOnFunction();
  99. }
  100. } else {
  101. if (command.toggleOffFunction) {
  102. command.toggleOffFunction();
  103. }
  104. }
  105. }