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