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