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