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