@whitesev/utils 2.9.10 → 2.9.12

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.12";
247
247
 
248
248
  /* eslint-disable */
249
249
  // ==UserScript==
@@ -1477,40 +1477,55 @@
1477
1477
  isNull(...args) {
1478
1478
  let result = true;
1479
1479
  const checkList = [...args];
1480
- for (const objItem of checkList) {
1481
- let itemResult = false;
1482
- if (objItem === null || objItem === undefined) {
1483
- itemResult = true;
1480
+ for (const obj of checkList) {
1481
+ let flag = false;
1482
+ if (obj === null || obj === undefined) {
1483
+ // null undefined
1484
+ flag = true;
1484
1485
  }
1485
1486
  else {
1486
- switch (typeof objItem) {
1487
+ switch (typeof obj) {
1487
1488
  case "object":
1488
- if (typeof objItem[Symbol.iterator] === "function") {
1489
- /* 可迭代 */
1490
- itemResult = objItem.length === 0;
1489
+ if (typeof obj[Symbol.iterator] === "function") {
1490
+ // 可迭代
1491
+ // Map Array NodeList HTMLCollection
1492
+ if (obj instanceof Map) {
1493
+ flag = obj.size === 0;
1494
+ }
1495
+ else {
1496
+ const length = obj.length;
1497
+ if (typeof length === "number") {
1498
+ flag = length === 0;
1499
+ }
1500
+ }
1491
1501
  }
1492
1502
  else {
1493
- itemResult = Object.keys(objItem).length === 0;
1503
+ if (obj?.toString() === "[object Object]") {
1504
+ // {}
1505
+ flag = Object.keys(obj).length === 0;
1506
+ }
1494
1507
  }
1495
1508
  break;
1496
1509
  case "number":
1497
- itemResult = objItem === 0;
1510
+ flag = isNaN(obj) ? true : obj === 0;
1498
1511
  break;
1499
- case "string":
1500
- itemResult = objItem.trim() === "" || objItem === "null" || objItem === "undefined";
1512
+ case "string": {
1513
+ const trimStr = obj.trim();
1514
+ flag = trimStr.trim() === "" || trimStr === "null" || trimStr === "undefined";
1501
1515
  break;
1516
+ }
1502
1517
  case "boolean":
1503
- itemResult = !objItem;
1518
+ flag = !obj;
1504
1519
  break;
1505
1520
  case "function": {
1506
- const funcStr = objItem.toString().replace(/\s/g, "");
1521
+ const funcStr = obj.toString().replace(/\s/g, "");
1507
1522
  /* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
1508
- itemResult = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
1523
+ flag = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
1509
1524
  break;
1510
1525
  }
1511
1526
  }
1512
1527
  }
1513
- result = result && itemResult;
1528
+ result = result && flag;
1514
1529
  }
1515
1530
  return result;
1516
1531
  }
@@ -5044,29 +5059,30 @@
5044
5059
  },
5045
5060
  };
5046
5061
  /**
5047
- * @param details 菜单配置
5062
+ * @param constructOptions 菜单配置
5048
5063
  */
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;
5064
+ constructor(constructOptions) {
5065
+ this.GM_Api.getValue = constructOptions.GM_getValue;
5066
+ this.GM_Api.setValue = constructOptions.GM_setValue;
5067
+ this.GM_Api.registerMenuCommand = constructOptions.GM_registerMenuCommand;
5068
+ this.GM_Api.unregisterMenuCommand = constructOptions.GM_unregisterMenuCommand;
5069
+ this.MenuHandle.$default.autoReload =
5070
+ typeof constructOptions.autoReload === "boolean" ? constructOptions.autoReload : true;
5055
5071
  for (const keyName of Object.keys(this.GM_Api)) {
5056
5072
  if (typeof this.GM_Api[keyName] !== "function") {
5057
5073
  throw new Error(`Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`);
5058
5074
  }
5059
5075
  }
5060
- this.add(details?.data || []);
5076
+ this.add(constructOptions?.data || []);
5061
5077
  }
5062
5078
  /**
5063
5079
  * 新增菜单数据
5064
- * @param menuOption
5080
+ * @param options
5065
5081
  */
5066
- __add(menuOption) {
5067
- if (Array.isArray(menuOption)) {
5068
- for (let index = 0; index < menuOption.length; index++) {
5069
- const option = menuOption[index];
5082
+ __add(options) {
5083
+ if (Array.isArray(options)) {
5084
+ for (let index = 0; index < options.length; index++) {
5085
+ const option = options[index];
5070
5086
  this.MenuHandle.$data.data.push({
5071
5087
  data: option,
5072
5088
  id: void 0,
@@ -5075,7 +5091,7 @@
5075
5091
  }
5076
5092
  else {
5077
5093
  this.MenuHandle.$data.data.push({
5078
- data: menuOption,
5094
+ data: options,
5079
5095
  id: void 0,
5080
5096
  });
5081
5097
  }
@@ -5084,11 +5100,21 @@
5084
5100
  * 新增菜单数据
5085
5101
  *
5086
5102
  * 自动调用.update()
5087
- * @param menuOption
5103
+ * @param options
5088
5104
  */
5089
- add(menuOption) {
5090
- this.__add(menuOption);
5105
+ add(options) {
5106
+ this.__add(options);
5091
5107
  this.update();
5108
+ return {
5109
+ destory: () => {
5110
+ if (!Array.isArray(options)) {
5111
+ options = [options];
5112
+ }
5113
+ options.forEach((option) => {
5114
+ this.delete(option.key);
5115
+ });
5116
+ },
5117
+ };
5092
5118
  }
5093
5119
  /**
5094
5120
  * 更新菜单数据
@@ -5108,14 +5134,14 @@
5108
5134
  else if (options != null) {
5109
5135
  menuOptionList = [...menuOptionList, options];
5110
5136
  }
5111
- menuOptionList.forEach((menuOption) => {
5112
- const oldMenuOption = this.MenuHandle.getMenuOption(menuOption.key);
5137
+ menuOptionList.forEach((option) => {
5138
+ const oldMenuOption = this.MenuHandle.getMenuOption(option.key);
5113
5139
  if (oldMenuOption) {
5114
5140
  // 覆盖
5115
- Object.assign(oldMenuOption, menuOption);
5141
+ Object.assign(oldMenuOption, option);
5116
5142
  }
5117
5143
  else {
5118
- this.__add(menuOption);
5144
+ this.__add(option);
5119
5145
  }
5120
5146
  });
5121
5147
  this.MenuHandle.$data.data.forEach((value) => {
@@ -5128,9 +5154,15 @@
5128
5154
  }
5129
5155
  /**
5130
5156
  * 卸载菜单
5131
- * @param menuId 已注册的菜单id
5157
+ * @param menuId 已注册的菜单id或者键名
5132
5158
  */
5133
5159
  delete(menuId) {
5160
+ if (typeof menuId === "string") {
5161
+ const __menuId = this.getMenuId(menuId);
5162
+ if (__menuId != null) {
5163
+ menuId = __menuId;
5164
+ }
5165
+ }
5134
5166
  this.GM_Api.unregisterMenuCommand(menuId);
5135
5167
  }
5136
5168
  /**
@@ -5152,7 +5184,9 @@
5152
5184
  * @param menuKey 菜单-键key
5153
5185
  */
5154
5186
  getShowTextValue(menuKey) {
5155
- return this.MenuHandle.getMenuHandledOption(menuKey).showText(this.getText(menuKey), this.getEnable(menuKey));
5187
+ const text = this.getText(menuKey);
5188
+ const enable = this.getEnable(menuKey);
5189
+ return this.MenuHandle.getMenuHandledOption(menuKey).showText(text, enable);
5156
5190
  }
5157
5191
  /**
5158
5192
  * 获取当前已注册菜单的id
@@ -5161,9 +5195,9 @@
5161
5195
  getMenuId(menuKey) {
5162
5196
  let result = null;
5163
5197
  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;
5198
+ const option = this.MenuHandle.$data.data[index];
5199
+ if (option.handleData?.key === menuKey) {
5200
+ result = option.id;
5167
5201
  break;
5168
5202
  }
5169
5203
  }