@whitesev/utils 2.9.10 → 2.9.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -237,7 +237,7 @@ const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
237
237
  const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
238
238
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
239
239
 
240
- const version = "2.9.10";
240
+ const version = "2.9.11";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -5038,29 +5038,30 @@ class GMMenu {
5038
5038
  },
5039
5039
  };
5040
5040
  /**
5041
- * @param details 菜单配置
5041
+ * @param constructOptions 菜单配置
5042
5042
  */
5043
- constructor(details) {
5044
- this.GM_Api.getValue = details.GM_getValue;
5045
- this.GM_Api.setValue = details.GM_setValue;
5046
- this.GM_Api.registerMenuCommand = details.GM_registerMenuCommand;
5047
- this.GM_Api.unregisterMenuCommand = details.GM_unregisterMenuCommand;
5048
- this.MenuHandle.$default.autoReload = typeof details.autoReload === "boolean" ? details.autoReload : true;
5043
+ constructor(constructOptions) {
5044
+ this.GM_Api.getValue = constructOptions.GM_getValue;
5045
+ this.GM_Api.setValue = constructOptions.GM_setValue;
5046
+ this.GM_Api.registerMenuCommand = constructOptions.GM_registerMenuCommand;
5047
+ this.GM_Api.unregisterMenuCommand = constructOptions.GM_unregisterMenuCommand;
5048
+ this.MenuHandle.$default.autoReload =
5049
+ typeof constructOptions.autoReload === "boolean" ? constructOptions.autoReload : true;
5049
5050
  for (const keyName of Object.keys(this.GM_Api)) {
5050
5051
  if (typeof this.GM_Api[keyName] !== "function") {
5051
5052
  throw new Error(`Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`);
5052
5053
  }
5053
5054
  }
5054
- this.add(details?.data || []);
5055
+ this.add(constructOptions?.data || []);
5055
5056
  }
5056
5057
  /**
5057
5058
  * 新增菜单数据
5058
- * @param menuOption
5059
+ * @param options
5059
5060
  */
5060
- __add(menuOption) {
5061
- if (Array.isArray(menuOption)) {
5062
- for (let index = 0; index < menuOption.length; index++) {
5063
- const option = menuOption[index];
5061
+ __add(options) {
5062
+ if (Array.isArray(options)) {
5063
+ for (let index = 0; index < options.length; index++) {
5064
+ const option = options[index];
5064
5065
  this.MenuHandle.$data.data.push({
5065
5066
  data: option,
5066
5067
  id: void 0,
@@ -5069,7 +5070,7 @@ class GMMenu {
5069
5070
  }
5070
5071
  else {
5071
5072
  this.MenuHandle.$data.data.push({
5072
- data: menuOption,
5073
+ data: options,
5073
5074
  id: void 0,
5074
5075
  });
5075
5076
  }
@@ -5078,11 +5079,21 @@ class GMMenu {
5078
5079
  * 新增菜单数据
5079
5080
  *
5080
5081
  * 自动调用.update()
5081
- * @param menuOption
5082
+ * @param options
5082
5083
  */
5083
- add(menuOption) {
5084
- this.__add(menuOption);
5084
+ add(options) {
5085
+ this.__add(options);
5085
5086
  this.update();
5087
+ return {
5088
+ destory: () => {
5089
+ if (!Array.isArray(options)) {
5090
+ options = [options];
5091
+ }
5092
+ options.forEach((option) => {
5093
+ this.delete(option.key);
5094
+ });
5095
+ },
5096
+ };
5086
5097
  }
5087
5098
  /**
5088
5099
  * 更新菜单数据
@@ -5102,14 +5113,14 @@ class GMMenu {
5102
5113
  else if (options != null) {
5103
5114
  menuOptionList = [...menuOptionList, options];
5104
5115
  }
5105
- menuOptionList.forEach((menuOption) => {
5106
- const oldMenuOption = this.MenuHandle.getMenuOption(menuOption.key);
5116
+ menuOptionList.forEach((option) => {
5117
+ const oldMenuOption = this.MenuHandle.getMenuOption(option.key);
5107
5118
  if (oldMenuOption) {
5108
5119
  // 覆盖
5109
- Object.assign(oldMenuOption, menuOption);
5120
+ Object.assign(oldMenuOption, option);
5110
5121
  }
5111
5122
  else {
5112
- this.__add(menuOption);
5123
+ this.__add(option);
5113
5124
  }
5114
5125
  });
5115
5126
  this.MenuHandle.$data.data.forEach((value) => {
@@ -5122,9 +5133,15 @@ class GMMenu {
5122
5133
  }
5123
5134
  /**
5124
5135
  * 卸载菜单
5125
- * @param menuId 已注册的菜单id
5136
+ * @param menuId 已注册的菜单id或者键名
5126
5137
  */
5127
5138
  delete(menuId) {
5139
+ if (typeof menuId === "string") {
5140
+ const __menuId = this.getMenuId(menuId);
5141
+ if (__menuId != null) {
5142
+ menuId = __menuId;
5143
+ }
5144
+ }
5128
5145
  this.GM_Api.unregisterMenuCommand(menuId);
5129
5146
  }
5130
5147
  /**
@@ -5146,7 +5163,9 @@ class GMMenu {
5146
5163
  * @param menuKey 菜单-键key
5147
5164
  */
5148
5165
  getShowTextValue(menuKey) {
5149
- return this.MenuHandle.getMenuHandledOption(menuKey).showText(this.getText(menuKey), this.getEnable(menuKey));
5166
+ const text = this.getText(menuKey);
5167
+ const enable = this.getEnable(menuKey);
5168
+ return this.MenuHandle.getMenuHandledOption(menuKey).showText(text, enable);
5150
5169
  }
5151
5170
  /**
5152
5171
  * 获取当前已注册菜单的id
@@ -5155,9 +5174,9 @@ class GMMenu {
5155
5174
  getMenuId(menuKey) {
5156
5175
  let result = null;
5157
5176
  for (let index = 0; index < this.MenuHandle.$data.data.length; index++) {
5158
- const optionData = this.MenuHandle.$data.data[index];
5159
- if (optionData.handleData.key === menuKey) {
5160
- result = optionData.id;
5177
+ const option = this.MenuHandle.$data.data[index];
5178
+ if (option.handleData?.key === menuKey) {
5179
+ result = option.id;
5161
5180
  break;
5162
5181
  }
5163
5182
  }