QuickMenu

油猴菜单库,支持开关菜单,支持状态保持,支持 Iframe

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/496315/1392531/QuickMenu.js

  1. // ==UserScript==
  2. // @name QuickMenu
  3. // @namespace https://github.com/JiyuShao/greasyfork-scripts
  4. // @version 2024-06-11
  5. // @description 油猴菜单库,支持开关菜单,支持状态保持,支持 Iframe
  6. // @author Jiyu Shao <jiyu.shao@gmail.com>
  7. // @grant GM_registerMenuCommand
  8. // @grant GM_unregisterMenuCommand
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_addValueChangeListener
  12. // ==/UserScript==
  13.  
  14. // 快捷生成菜单逻辑
  15. const QuickMenu = {
  16. isInited: false,
  17. label: '',
  18. isUpdating: false,
  19. stateConfigMap: {}, // 状态数据
  20. storeConfigMap: {}, // 缓存数据
  21. init: function () {
  22. if (this.isInited) {
  23. return;
  24. }
  25. this.isInited = true;
  26.  
  27. // 初始化 label
  28. if (!this.label) {
  29. let level = 0;
  30. let currentWindow = window;
  31. while (currentWindow.parent && currentWindow !== currentWindow.parent) {
  32. // 增加计数器,因为我们进入了更深层的嵌套
  33. level++;
  34. // 移动到父级窗口
  35. currentWindow = currentWindow.parent;
  36. // 可选:设置一个最大嵌套层数以避免无限循环
  37. if (level > 10) {
  38. // 这个数字可以根据实际情况调整
  39. console.warn('可能存在无限循环的iframe嵌套,停止计数');
  40. break;
  41. }
  42. }
  43. // 如果level大于0,我们至少在一个嵌套的iframe中
  44. const currentOrderMap = GM_getValue('QM_ORDER_MAP') || {};
  45. const currentOrder = (currentOrderMap[level] || -1) + 1;
  46. GM_setValue('QM_ORDER_MAP', {
  47. ...currentOrderMap,
  48. [level]: currentOrder,
  49. });
  50. this.label = `第${level}层第${currentOrder}个`;
  51. }
  52.  
  53. // 添加更新监听回调,保证菜单展示正确,不需要销毁
  54. GM_addValueChangeListener(
  55. 'QM_TRIGGER_UPDATE',
  56. (_key, _oldValue, _newValue, remote) => {
  57. console.log('[QuickMenu] QM_TRIGGER_UPDATE', {
  58. currentLabel: this.label,
  59. remote,
  60. oldValue: _oldValue,
  61. newValue: _newValue,
  62. });
  63. if (remote) {
  64. this._update({
  65. useStore: true, // 表示触发源是远程其他模块,需要从 store 中获取数据
  66. triggerCallback: true, // 需要重新执行回调刷新逻辑
  67. triggerRemote: false, // 不需要再次触发远程更新
  68. });
  69. }
  70. }
  71. );
  72. },
  73. // 存储菜单
  74. setMenuConfigStore: function () {
  75. Object.values(this.stateConfigMap).forEach((e) => {
  76. this.storeConfigMap[e.name] = {
  77. value: e.value,
  78. };
  79. });
  80. GM_setValue('QM_MENU', this.storeConfigMap);
  81. // 触发其他实例进行更新
  82. GM_setValue('QM_TRIGGER_UPDATE', `${this.label}:${Math.random()}`);
  83. },
  84. // 获取菜单配置
  85. getMenuConfigStore: function () {
  86. // 初始化 store 数据
  87. this.storeConfigMap = GM_getValue('QM_MENU') || {};
  88. },
  89. clearStore: function () {
  90. // 清空 store 数据
  91. GM_setValue('QM_MENU', undefined);
  92. this._update({
  93. useStore: true, // 使用 store 数据,只更新当前环境
  94. triggerCallback: true, // 当前环境也要执行回调
  95. triggerRemote: true, // 需要再次触发远程更新
  96. });
  97. },
  98. // 添加菜单配置
  99. add: function (config) {
  100. this.init();
  101. // 兼容数组配置
  102. if (Array.isArray(config)) {
  103. config.forEach((e) => this.add(e));
  104. for (var i in config) {
  105. this.add(config[i]);
  106. }
  107. return;
  108. }
  109. // 检查配置名称
  110. if (!config.name && typeof config === 'object') {
  111. alert('QM_MENU.add Config name is need.');
  112. return;
  113. }
  114. // 添加到状态配置数据中
  115. this.stateConfigMap[config.name] = {
  116. ...config,
  117. isInited: false, // 需要执行回调初始化执行的逻辑
  118. };
  119.  
  120. // 执行更新的逻辑
  121. if (!this.isUpdating) {
  122. this.isUpdating = true;
  123. // 这里放到宏任务队列中执行,批量更新
  124. setTimeout(() => {
  125. this.isUpdating = false;
  126. // 更新数据 & UI
  127. this._update();
  128. }, 0);
  129. }
  130. },
  131. // 更新状态数据
  132. _updateState: function (options) {
  133. const { useStore = false } = options || {};
  134. this.getMenuConfigStore();
  135. Object.values(this.stateConfigMap).forEach((currentConfig) => {
  136. let menuDisplay = currentConfig.name;
  137. // 为 Toggle 定制展示名称
  138. if (currentConfig.type === 'toggle') {
  139. // 使用 store 里的缓存值,有以下两种情况:
  140. // 1. 当前配置还没初始化
  141. // 2. 由于会有多实例的情况,useStore 的话以 store 数据为准
  142. if (!currentConfig.isInited || useStore) {
  143. const currentStoreConfig = this.storeConfigMap[currentConfig.name];
  144. currentConfig.value =
  145. currentStoreConfig && currentStoreConfig.value
  146. ? currentStoreConfig.value
  147. : 'off';
  148. }
  149.  
  150. // 如果没有值的话,默认为 off
  151. currentConfig.value = currentConfig.value ? currentConfig.value : 'off';
  152. menuDisplay = `${menuDisplay}[${
  153. currentConfig.value === 'on' ? 'x' : ' '
  154. }]`;
  155. }
  156. });
  157.  
  158. console.debug(`[QuickMenu] ${this.label}: 状态已更新`, {
  159. options,
  160. stateConfigMap: this.stateConfigMap,
  161. });
  162. },
  163. // 更新菜单、执行初始化回调、保存 store、触发远程更新
  164. _commitUpdate: function (options) {
  165. const { triggerCallback = false, triggerRemote = true } = options || {};
  166. Object.values(this.stateConfigMap).forEach((currentConfig) => {
  167. // 判断是否可以执行菜单回调
  168. let runCallbackFlag = false;
  169. if (typeof currentConfig.shouldInitRun === 'boolean') {
  170. runCallbackFlag = currentConfig.shouldInitRun;
  171. } else if (typeof currentConfig.shouldInitRun === 'function') {
  172. runCallbackFlag = !!currentConfig.shouldInitRun.call(null);
  173. }
  174. // 判断是否需要注入菜单
  175. let updateMenuFlag = true;
  176. if (typeof currentConfig.shouldAddMenu === 'boolean') {
  177. updateMenuFlag = currentConfig.shouldAddMenu;
  178. } else if (typeof currentConfig.shouldAddMenu === 'function') {
  179. updateMenuFlag = !!currentConfig.shouldAddMenu.call(null);
  180. }
  181. // 生成菜单名称
  182. let menuDisplay = currentConfig.name;
  183. if (currentConfig.type === 'toggle') {
  184. // 如果没有值的话,默认为 off
  185. currentConfig.value = currentConfig.value ? currentConfig.value : 'off';
  186. menuDisplay = `${menuDisplay}[${
  187. currentConfig.value === 'on' ? 'x' : ' '
  188. }]`;
  189. }
  190. // 执行回调有两个时机
  191. // 1. 初始化时
  192. // 2. 接收到远程更新时
  193. if ((!currentConfig.isInited || triggerCallback) && runCallbackFlag) {
  194. currentConfig.isInited = true;
  195. currentConfig.callback &&
  196. currentConfig.callback.call(null, currentConfig.value);
  197. }
  198. // 有时候需要更新菜单,所以这里先卸载
  199. if (currentConfig.id) {
  200. GM_unregisterMenuCommand(currentConfig.id); // 删除菜单
  201. delete currentConfig.id;
  202. }
  203. if (updateMenuFlag) {
  204. currentConfig.id = GM_registerMenuCommand(
  205. menuDisplay,
  206. () => {
  207. console.debug(`[QuickMenu] ${this.label}:点击${menuDisplay}`);
  208. // 切换 value,并更新,实际执行时只有 toggle 的值会更新
  209. currentConfig.value = { on: 'off', off: 'on' }[currentConfig.value];
  210. // 使用最新的 value 执行用户回调
  211. currentConfig.callback &&
  212. currentConfig.callback.call(null, currentConfig.value);
  213. // 放到最后更新,因为用户回调有可能会影响数据,如清空 Store
  214. this._update();
  215. },
  216. { autoClose: false }
  217. );
  218. }
  219. });
  220. // 远程的不需要更新数据,只需要注册(不可用)菜单
  221. if (triggerRemote) {
  222. this.setMenuConfigStore();
  223. }
  224. console.debug(`[QuickMenu] ${this.label}: 更新已提交`);
  225. },
  226. // 触发更新
  227. _update: function (options) {
  228. this._updateState(options);
  229. this._commitUpdate(options);
  230. },
  231. };

QingJ © 2025

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