@whitesev/utils 2.6.8 → 2.6.9

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/src/Utils.ts CHANGED
@@ -30,6 +30,8 @@ import {
30
30
  setTimeout as WorkerSetTimeout,
31
31
  } from "worker-timers";
32
32
  import { ModuleRaid } from "./ModuleRaid";
33
+ import { domUtils } from "./DOMUtils";
34
+ import { CommonUtil } from "./CommonUtil";
33
35
 
34
36
  class Utils {
35
37
  private windowApi: typeof WindowApi.prototype;
@@ -37,8 +39,7 @@ class Utils {
37
39
  this.windowApi = new WindowApi(option);
38
40
  }
39
41
  /** 版本号 */
40
- version = "2025.5.28";
41
-
42
+ version = "2025.6.7";
42
43
  /**
43
44
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
44
45
  * @param cssText css字符串
@@ -91,75 +92,7 @@ class Utils {
91
92
  }
92
93
  }
93
94
  */
94
- assign<T1, T2 extends object, T3 extends boolean>(
95
- target: T1,
96
- source: T2,
97
- isAdd?: T3
98
- ): T3 extends true ? T1 & T2 : T1;
99
- assign(target = {}, source = {}, isAdd = false) {
100
- let UtilsContext = this;
101
- if (Array.isArray(source)) {
102
- let canTraverse = source.filter((item) => {
103
- return typeof item === "object";
104
- });
105
- if (!canTraverse.length) {
106
- return source;
107
- }
108
- }
109
- if (source == null) {
110
- return target;
111
- }
112
- if (target == null) {
113
- target = {};
114
- }
115
- if (isAdd) {
116
- for (const sourceKeyName in source) {
117
- const targetKeyName = sourceKeyName;
118
- let targetValue = (target as any)[targetKeyName];
119
- let sourceValue = (source as any)[sourceKeyName];
120
- if (
121
- typeof sourceValue === "object" &&
122
- sourceValue != null &&
123
- sourceKeyName in target &&
124
- !UtilsContext.isDOM(sourceValue)
125
- ) {
126
- /* 源端的值是object类型,且不是元素节点 */
127
- (target as any)[sourceKeyName] = UtilsContext.assign(
128
- targetValue,
129
- sourceValue,
130
- isAdd
131
- );
132
- continue;
133
- }
134
- (target as any)[sourceKeyName] = sourceValue;
135
- }
136
- } else {
137
- for (const targetKeyName in target) {
138
- if (targetKeyName in source) {
139
- let targetValue = (target as any)[targetKeyName];
140
- let sourceValue = (source as any)[targetKeyName];
141
- if (
142
- typeof sourceValue === "object" &&
143
- sourceValue != null &&
144
- !UtilsContext.isDOM(sourceValue) &&
145
- Object.keys(sourceValue).length
146
- ) {
147
- /* 源端的值是object类型,且不是元素节点 */
148
- (target as any)[targetKeyName] = UtilsContext.assign(
149
- targetValue,
150
- sourceValue,
151
- isAdd
152
- );
153
- continue;
154
- }
155
- /* 直接赋值 */
156
- (target as any)[targetKeyName] = sourceValue;
157
- }
158
- }
159
- }
160
-
161
- return target;
162
- }
95
+ assign = CommonUtil.assign.bind(CommonUtil);
163
96
  /**
164
97
  * 异步替换字符串
165
98
  * @param string 需要被替换的目标字符串
@@ -404,18 +337,7 @@ class Utils {
404
337
  * 深拷贝
405
338
  * @param obj 对象
406
339
  */
407
- deepClone<T extends object | undefined | null>(obj?: T): T;
408
- deepClone<T extends object | undefined | null>(obj?: T) {
409
- let UtilsContext = this;
410
- if (obj === void 0) return void 0;
411
- if (obj === null) return null;
412
- let clone = obj instanceof Array ? [] : {};
413
- for (const [key, value] of Object.entries(obj)) {
414
- (clone as any)[key] =
415
- typeof value === "object" ? UtilsContext.deepClone(value) : value;
416
- }
417
- return clone;
418
- }
340
+ deepClone = CommonUtil.deepClone.bind(CommonUtil);
419
341
  /**
420
342
  * 防抖函数
421
343
  * @param fn 需要触发的回调
@@ -469,7 +391,10 @@ class Utils {
469
391
  );
470
392
  }
471
393
  let result = false;
472
- let needRemoveDOM = (element as HTMLElement).closest(targetSelector);
394
+ let needRemoveDOM = domUtils.closest(
395
+ element as HTMLElement,
396
+ targetSelector
397
+ );
473
398
  if (needRemoveDOM) {
474
399
  needRemoveDOM.remove();
475
400
  result = true;
@@ -1954,10 +1879,7 @@ class Utils {
1954
1879
  * Utils.isDOM(document.querySelector("a"))
1955
1880
  * > true
1956
1881
  */
1957
- isDOM(target: any): boolean;
1958
- isDOM(target: any): boolean {
1959
- return target instanceof Node;
1960
- }
1882
+ isDOM = CommonUtil.isDOM.bind(CommonUtil);
1961
1883
  /**
1962
1884
  * 判断浏览器是否支持全屏
1963
1885
  */
@@ -2189,12 +2111,7 @@ class Utils {
2189
2111
  * Utils.isNotNull("123");
2190
2112
  * > true
2191
2113
  */
2192
- isNotNull<T>(value: T | null | undefined): value is T;
2193
- isNotNull(...args: any[]): boolean;
2194
- isNotNull(...args: any[]): boolean {
2195
- let UtilsContext = this;
2196
- return !UtilsContext.isNull.apply(this, args);
2197
- }
2114
+ isNotNull = CommonUtil.isNotNull.bind(CommonUtil);
2198
2115
  /**
2199
2116
  * 判断对象或数据是否为空
2200
2117
  * + `String`判空的值,如 ""、"null"、"undefined"、" "
@@ -2240,51 +2157,7 @@ class Utils {
2240
2157
  Utils.isNull(false,[123]);
2241
2158
  > false
2242
2159
  **/
2243
- isNull<T>(value: T | undefined | null): value is null | undefined;
2244
- isNull(...args: any[]): boolean;
2245
- isNull(...args: any[]): boolean {
2246
- let result = true;
2247
- let checkList = [...args];
2248
- for (const objItem of checkList) {
2249
- let itemResult = false;
2250
- if (objItem === null || objItem === undefined) {
2251
- itemResult = true;
2252
- } else {
2253
- switch (typeof objItem) {
2254
- case "object":
2255
- if (typeof objItem[Symbol.iterator] === "function") {
2256
- /* 可迭代 */
2257
- itemResult = objItem.length === 0;
2258
- } else {
2259
- itemResult = Object.keys(objItem).length === 0;
2260
- }
2261
- break;
2262
- case "number":
2263
- itemResult = objItem === 0;
2264
- break;
2265
- case "string":
2266
- itemResult =
2267
- objItem.trim() === "" ||
2268
- objItem === "null" ||
2269
- objItem === "undefined";
2270
- break;
2271
- case "boolean":
2272
- itemResult = !objItem;
2273
- break;
2274
- case "function":
2275
- let funcStr = objItem.toString().replace(/\s/g, "");
2276
- /* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
2277
- itemResult = Boolean(
2278
- funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/)
2279
- );
2280
- break;
2281
- }
2282
- }
2283
- result = result && itemResult;
2284
- }
2285
-
2286
- return result;
2287
- }
2160
+ isNull = CommonUtil.isNull.bind(CommonUtil);
2288
2161
 
2289
2162
  /**
2290
2163
  * 判断浏览器主题是否是暗黑|深色模式
@@ -3583,7 +3456,7 @@ class Utils {
3583
3456
  }
3584
3457
  let sliderElement =
3585
3458
  typeof selector === "string"
3586
- ? this.windowApi.document.querySelector(selector)
3459
+ ? domUtils.selector<HTMLElement>(selector)
3587
3460
  : selector;
3588
3461
  if (
3589
3462
  !(sliderElement instanceof Node) ||
@@ -3902,61 +3775,7 @@ class Utils {
3902
3775
  * Utils.toJSON("{123:123}")
3903
3776
  * > {123:123}
3904
3777
  */
3905
- toJSON<T = any>(
3906
- data: string | null,
3907
- errorCallBack?: (error: Error) => void
3908
- ): T;
3909
- toJSON<T = any>(
3910
- data: string | null,
3911
- errorCallBack?: (error: Error) => void
3912
- ): T {
3913
- let UtilsContext = this;
3914
- let result: any = {};
3915
- if (typeof data === "object") {
3916
- return data as any;
3917
- }
3918
- UtilsContext.tryCatch()
3919
- .config({ log: false })
3920
- .error((error: Error) => {
3921
- UtilsContext.tryCatch()
3922
- .error(() => {
3923
- try {
3924
- result = (UtilsContext.windowApi.window as any).eval(
3925
- "(" + data + ")"
3926
- );
3927
- } catch (error2: any) {
3928
- if (typeof errorCallBack === "function") {
3929
- errorCallBack(error2);
3930
- }
3931
- }
3932
- })
3933
- .run(() => {
3934
- if (
3935
- data &&
3936
- /^[\],:{}\s]*$/.test(
3937
- data
3938
- .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
3939
- .replace(
3940
- /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
3941
- "]"
3942
- )
3943
- .replace(/(?:^|:|,)(?:\s*\[)+/g, "")
3944
- )
3945
- ) {
3946
- result = new Function("return " + data)();
3947
- } else {
3948
- if (typeof errorCallBack === "function") {
3949
- errorCallBack(new Error("target is not a JSON"));
3950
- }
3951
- }
3952
- });
3953
- })
3954
- .run(() => {
3955
- data = (data as string).trim();
3956
- result = JSON.parse(data);
3957
- });
3958
- return result as any;
3959
- }
3778
+ toJSON = CommonUtil.toJSON.bind(CommonUtil);
3960
3779
  /**
3961
3780
  * 对象转为UrlSearchParams格式的字符串
3962
3781
  * @param obj 目标对象,可以是对象组成的数组
@@ -4393,7 +4212,7 @@ class Utils {
4393
4212
  if (Array.isArray(selector)) {
4394
4213
  let result: T[] = [];
4395
4214
  for (let index = 0; index < selector.length; index++) {
4396
- let node = parent.querySelector(selector[index]);
4215
+ let node = domUtils.selector(selector[index]);
4397
4216
  if (node) {
4398
4217
  result.push(node as any);
4399
4218
  }
@@ -4404,7 +4223,7 @@ class Utils {
4404
4223
  } else if (typeof selector === "function") {
4405
4224
  return selector();
4406
4225
  } else {
4407
- return parent.querySelector(selector);
4226
+ return domUtils.selector(selector, parent);
4408
4227
  }
4409
4228
  }
4410
4229
  return UtilsContext.wait(
@@ -4704,18 +4523,16 @@ class Utils {
4704
4523
  if (Array.isArray(selector)) {
4705
4524
  let result: T[] = [];
4706
4525
  for (let index = 0; index < selector.length; index++) {
4707
- let nodeList = (parent as Element).querySelectorAll(
4708
- selector[index]
4709
- ) as T;
4526
+ let nodeList = domUtils.selectorAll(selector[index], parent);
4710
4527
  if (nodeList.length) {
4711
- result.push(nodeList);
4528
+ result.push(nodeList as any as T);
4712
4529
  }
4713
4530
  }
4714
4531
  if (result.length === selector.length) {
4715
4532
  return result;
4716
4533
  }
4717
4534
  } else {
4718
- let nodeList = (parent as Element).querySelectorAll(selector) as T;
4535
+ let nodeList = domUtils.selectorAll(selector, parent);
4719
4536
  if (nodeList.length) {
4720
4537
  return nodeList;
4721
4538
  }
@@ -5212,17 +5029,7 @@ class Utils {
5212
5029
  * @param target 需要覆盖的对象
5213
5030
  * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
5214
5031
  */
5215
- coverObjectFunctionThis(target: any, objectThis?: any) {
5216
- if (typeof target !== "object" || target === null) {
5217
- throw new Error("target must be object");
5218
- }
5219
- objectThis = objectThis || target;
5220
- Object.keys(target).forEach((key) => {
5221
- if (typeof target[key] === "function") {
5222
- target[key] = target[key].bind(objectThis);
5223
- }
5224
- });
5225
- }
5032
+ coverObjectFunctionThis = CommonUtil.coverObjectFunctionThis.bind(CommonUtil);
5226
5033
  /**
5227
5034
  * 生成uuid
5228
5035
  * @example
@@ -1,3 +1,4 @@
1
+ import { CommonUtil } from "./CommonUtil";
1
2
  import type {
2
3
  UtilsGMCookieDeleteOptions,
3
4
  UtilsGMCookieListOptions,
@@ -5,7 +6,6 @@ import type {
5
6
  UtilsGMCookieSetOptions,
6
7
  WindowApiOption,
7
8
  } from "./types/UtilsGMCookie";
8
- import { Utils } from "./Utils";
9
9
 
10
10
  export class UtilsGMCookie {
11
11
  private windowApi = {
@@ -82,7 +82,7 @@ export class UtilsGMCookie {
82
82
  name: "",
83
83
  path: "/",
84
84
  };
85
- defaultOption = Utils.assign(defaultOption, option);
85
+ defaultOption = CommonUtil.assign(defaultOption, option);
86
86
  let cookies = this.getCookiesList();
87
87
  cookies.forEach((item) => {
88
88
  item = item.trim();
@@ -133,7 +133,7 @@ export class UtilsGMCookie {
133
133
  name: "",
134
134
  path: "/",
135
135
  };
136
- defaultOption = Utils.assign(defaultOption, option);
136
+ defaultOption = CommonUtil.assign(defaultOption, option);
137
137
  let cookies = this.getCookiesList();
138
138
  cookies.forEach((item) => {
139
139
  item = item.trim();
@@ -183,7 +183,7 @@ export class UtilsGMCookie {
183
183
  */
184
184
  expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
185
185
  };
186
- defaultOption = Utils.assign(defaultOption, option);
186
+ defaultOption = CommonUtil.assign(defaultOption, option);
187
187
  let life = defaultOption.expirationDate
188
188
  ? defaultOption.expirationDate
189
189
  : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
@@ -194,7 +194,7 @@ export class UtilsGMCookie {
194
194
  ";expires=" +
195
195
  (new Date(life) as any).toGMTString() +
196
196
  "; path=/";
197
- if (Utils.isNotNull(defaultOption.domain)) {
197
+ if (CommonUtil.isNull(defaultOption.domain)) {
198
198
  cookieStr += "; domain=" + defaultOption.domain;
199
199
  }
200
200
  this.windowApi.document.cookie = cookieStr;
@@ -223,9 +223,9 @@ export class UtilsGMCookie {
223
223
  path: "/",
224
224
  firstPartyDomain: "",
225
225
  };
226
- defaultOption = Utils.assign(defaultOption, option);
226
+ defaultOption = CommonUtil.assign(defaultOption, option);
227
227
  let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
228
- if (Utils.isNotNull(defaultOption.firstPartyDomain)) {
228
+ if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
229
229
  cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
230
230
  }
231
231
  this.windowApi.document.cookie = cookieStr;
@@ -1,9 +1,9 @@
1
+ import { CommonUtil } from "./CommonUtil";
1
2
  import type {
2
3
  UtilsGMMenuConstructorOptions,
3
4
  UtilsGMMenuOption,
4
5
  UtilsGMMenuOptionData,
5
6
  } from "./types/UtilsGMMenu";
6
- import { Utils } from "./Utils";
7
7
 
8
8
  class GMMenu {
9
9
  private GM_Api = {
@@ -87,7 +87,7 @@ class GMMenu {
87
87
  menuOptions = [menuOptions];
88
88
  }
89
89
  for (let index = 0; index < menuOptions.length; index++) {
90
- let cloneMenuOptionData = Utils.deepClone(menuOptions[index].data);
90
+ let cloneMenuOptionData = CommonUtil.deepClone(menuOptions[index].data);
91
91
  const { showText, clickCallBack } = this.handleMenuData(
92
92
  cloneMenuOptionData as Required<UtilsGMMenuOption>
93
93
  );