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