@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.
@@ -242,7 +242,7 @@ System.register('Utils', [], (function (exports) {
242
242
  const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
243
243
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
244
244
 
245
- const version = "2.9.10";
245
+ const version = "2.9.11";
246
246
 
247
247
  /* eslint-disable */
248
248
  // ==UserScript==
@@ -5043,29 +5043,30 @@ System.register('Utils', [], (function (exports) {
5043
5043
  },
5044
5044
  };
5045
5045
  /**
5046
- * @param details 菜单配置
5046
+ * @param constructOptions 菜单配置
5047
5047
  */
5048
- constructor(details) {
5049
- this.GM_Api.getValue = details.GM_getValue;
5050
- this.GM_Api.setValue = details.GM_setValue;
5051
- this.GM_Api.registerMenuCommand = details.GM_registerMenuCommand;
5052
- this.GM_Api.unregisterMenuCommand = details.GM_unregisterMenuCommand;
5053
- this.MenuHandle.$default.autoReload = typeof details.autoReload === "boolean" ? details.autoReload : true;
5048
+ constructor(constructOptions) {
5049
+ this.GM_Api.getValue = constructOptions.GM_getValue;
5050
+ this.GM_Api.setValue = constructOptions.GM_setValue;
5051
+ this.GM_Api.registerMenuCommand = constructOptions.GM_registerMenuCommand;
5052
+ this.GM_Api.unregisterMenuCommand = constructOptions.GM_unregisterMenuCommand;
5053
+ this.MenuHandle.$default.autoReload =
5054
+ typeof constructOptions.autoReload === "boolean" ? constructOptions.autoReload : true;
5054
5055
  for (const keyName of Object.keys(this.GM_Api)) {
5055
5056
  if (typeof this.GM_Api[keyName] !== "function") {
5056
5057
  throw new Error(`Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`);
5057
5058
  }
5058
5059
  }
5059
- this.add(details?.data || []);
5060
+ this.add(constructOptions?.data || []);
5060
5061
  }
5061
5062
  /**
5062
5063
  * 新增菜单数据
5063
- * @param menuOption
5064
+ * @param options
5064
5065
  */
5065
- __add(menuOption) {
5066
- if (Array.isArray(menuOption)) {
5067
- for (let index = 0; index < menuOption.length; index++) {
5068
- const option = menuOption[index];
5066
+ __add(options) {
5067
+ if (Array.isArray(options)) {
5068
+ for (let index = 0; index < options.length; index++) {
5069
+ const option = options[index];
5069
5070
  this.MenuHandle.$data.data.push({
5070
5071
  data: option,
5071
5072
  id: void 0,
@@ -5074,7 +5075,7 @@ System.register('Utils', [], (function (exports) {
5074
5075
  }
5075
5076
  else {
5076
5077
  this.MenuHandle.$data.data.push({
5077
- data: menuOption,
5078
+ data: options,
5078
5079
  id: void 0,
5079
5080
  });
5080
5081
  }
@@ -5083,11 +5084,21 @@ System.register('Utils', [], (function (exports) {
5083
5084
  * 新增菜单数据
5084
5085
  *
5085
5086
  * 自动调用.update()
5086
- * @param menuOption
5087
+ * @param options
5087
5088
  */
5088
- add(menuOption) {
5089
- this.__add(menuOption);
5089
+ add(options) {
5090
+ this.__add(options);
5090
5091
  this.update();
5092
+ return {
5093
+ destory: () => {
5094
+ if (!Array.isArray(options)) {
5095
+ options = [options];
5096
+ }
5097
+ options.forEach((option) => {
5098
+ this.delete(option.key);
5099
+ });
5100
+ },
5101
+ };
5091
5102
  }
5092
5103
  /**
5093
5104
  * 更新菜单数据
@@ -5107,14 +5118,14 @@ System.register('Utils', [], (function (exports) {
5107
5118
  else if (options != null) {
5108
5119
  menuOptionList = [...menuOptionList, options];
5109
5120
  }
5110
- menuOptionList.forEach((menuOption) => {
5111
- const oldMenuOption = this.MenuHandle.getMenuOption(menuOption.key);
5121
+ menuOptionList.forEach((option) => {
5122
+ const oldMenuOption = this.MenuHandle.getMenuOption(option.key);
5112
5123
  if (oldMenuOption) {
5113
5124
  // 覆盖
5114
- Object.assign(oldMenuOption, menuOption);
5125
+ Object.assign(oldMenuOption, option);
5115
5126
  }
5116
5127
  else {
5117
- this.__add(menuOption);
5128
+ this.__add(option);
5118
5129
  }
5119
5130
  });
5120
5131
  this.MenuHandle.$data.data.forEach((value) => {
@@ -5127,9 +5138,15 @@ System.register('Utils', [], (function (exports) {
5127
5138
  }
5128
5139
  /**
5129
5140
  * 卸载菜单
5130
- * @param menuId 已注册的菜单id
5141
+ * @param menuId 已注册的菜单id或者键名
5131
5142
  */
5132
5143
  delete(menuId) {
5144
+ if (typeof menuId === "string") {
5145
+ const __menuId = this.getMenuId(menuId);
5146
+ if (__menuId != null) {
5147
+ menuId = __menuId;
5148
+ }
5149
+ }
5133
5150
  this.GM_Api.unregisterMenuCommand(menuId);
5134
5151
  }
5135
5152
  /**
@@ -5151,7 +5168,9 @@ System.register('Utils', [], (function (exports) {
5151
5168
  * @param menuKey 菜单-键key
5152
5169
  */
5153
5170
  getShowTextValue(menuKey) {
5154
- return this.MenuHandle.getMenuHandledOption(menuKey).showText(this.getText(menuKey), this.getEnable(menuKey));
5171
+ const text = this.getText(menuKey);
5172
+ const enable = this.getEnable(menuKey);
5173
+ return this.MenuHandle.getMenuHandledOption(menuKey).showText(text, enable);
5155
5174
  }
5156
5175
  /**
5157
5176
  * 获取当前已注册菜单的id
@@ -5160,9 +5179,9 @@ System.register('Utils', [], (function (exports) {
5160
5179
  getMenuId(menuKey) {
5161
5180
  let result = null;
5162
5181
  for (let index = 0; index < this.MenuHandle.$data.data.length; index++) {
5163
- const optionData = this.MenuHandle.$data.data[index];
5164
- if (optionData.handleData.key === menuKey) {
5165
- result = optionData.id;
5182
+ const option = this.MenuHandle.$data.data[index];
5183
+ if (option.handleData?.key === menuKey) {
5184
+ result = option.id;
5166
5185
  break;
5167
5186
  }
5168
5187
  }