@whitesev/utils 2.5.6 → 2.5.8

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
@@ -22,6 +22,7 @@ import {
22
22
  type UtilsOwnObject,
23
23
  } from "./types/global";
24
24
  import type { WindowApiOption } from "./types/WindowApi";
25
+ import { ModuleRaid } from "./ModuleRaid";
25
26
 
26
27
  class Utils {
27
28
  private windowApi: typeof WindowApi.prototype;
@@ -29,7 +30,7 @@ class Utils {
29
30
  this.windowApi = new WindowApi(option);
30
31
  }
31
32
  /** 版本号 */
32
- version = "2024.12.25";
33
+ version = "2025.1.11";
33
34
 
34
35
  /**
35
36
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -4095,6 +4096,113 @@ class Utils {
4095
4096
  })
4096
4097
  );
4097
4098
  }
4099
+ /**
4100
+ * 等待任意事件成立
4101
+ *
4102
+ * 运行方式为根据页面元素的改变而触发回调
4103
+ * @param checkFn 检测的函数
4104
+ * @param timeout 超时时间,默认0
4105
+ * @param parent (可选)父元素,默认document
4106
+ * @example
4107
+ * Utils.wait(()=> {
4108
+ * let $test = document.querySelector("#test");
4109
+ * return {
4110
+ * success: $test !== null,
4111
+ * data: $test
4112
+ * }
4113
+ * })
4114
+ */
4115
+ wait<T extends any>(
4116
+ checkFn: (...args: any[]) => {
4117
+ /**
4118
+ * 是否检测成功
4119
+ */
4120
+ success: boolean;
4121
+ /**
4122
+ * 返回的值
4123
+ */
4124
+ data: T;
4125
+ },
4126
+ timeout?: null | undefined,
4127
+ parent?: Node | Element | Document | HTMLElement
4128
+ ): Promise<T>;
4129
+ wait<T extends any>(
4130
+ checkFn: (...args: any[]) => {
4131
+ /**
4132
+ * 是否检测成功
4133
+ */
4134
+ success: boolean;
4135
+ /**
4136
+ * 返回的值
4137
+ */
4138
+ data: T;
4139
+ },
4140
+ timeout?: number,
4141
+ parent?: Node | Element | Document | HTMLElement
4142
+ ): Promise<T | null>;
4143
+ wait<T extends any>(
4144
+ checkFn: (...args: any[]) => {
4145
+ /**
4146
+ * 是否检测成功
4147
+ */
4148
+ success: boolean;
4149
+ /**
4150
+ * 返回的值
4151
+ */
4152
+ data: T;
4153
+ },
4154
+ timeout?: number | null | undefined,
4155
+ parent?: Node | Element | Document | HTMLElement
4156
+ ): Promise<T | null> {
4157
+ const UtilsContext = this;
4158
+ let __timeout__ = typeof timeout === "number" ? timeout : 0;
4159
+ return new Promise((resolve) => {
4160
+ let observer = UtilsContext.mutationObserver(
4161
+ parent || UtilsContext.windowApi.document,
4162
+ {
4163
+ config: {
4164
+ subtree: true,
4165
+ childList: true,
4166
+ attributes: true,
4167
+ },
4168
+ immediate: true,
4169
+ callback(mutations, __observer__) {
4170
+ let result = checkFn();
4171
+ if (result.success) {
4172
+ // 取消观察器
4173
+ if (typeof __observer__?.disconnect === "function") {
4174
+ __observer__.disconnect();
4175
+ }
4176
+ resolve(result.data);
4177
+ }
4178
+ },
4179
+ }
4180
+ );
4181
+ if (__timeout__ > 0) {
4182
+ setTimeout(() => {
4183
+ // 取消观察器
4184
+ if (typeof observer?.disconnect === "function") {
4185
+ observer.disconnect();
4186
+ }
4187
+ resolve(null as T);
4188
+ }, __timeout__);
4189
+ }
4190
+ });
4191
+ }
4192
+ /**
4193
+ * 等待元素出现
4194
+ * @param selectorFn 获取元素的函数
4195
+ * @param timeout 超时时间,默认0
4196
+ * @example
4197
+ * Utils.waitNode(()=>document.querySelector("div"), 1000).then( $div =>{
4198
+ * console.log($div); // $div => HTMLDivELement | null
4199
+ * })
4200
+ */
4201
+ waitNode<K extends any>(selectorFn: () => K | null | undefined): Promise<K>;
4202
+ waitNode<K extends any>(
4203
+ selectorFn: () => K | null | undefined,
4204
+ timeout: number
4205
+ ): Promise<K | null | undefined>;
4098
4206
  /**
4099
4207
  * 等待元素出现
4100
4208
  * @param selector CSS选择器
@@ -4103,7 +4211,7 @@ class Utils {
4103
4211
  * Utils.waitNode("div").then( $div =>{
4104
4212
  * console.log($div); // div => HTMLDivELement
4105
4213
  * })
4106
- * Utils.waitNode("div",document).then( $div =>{
4214
+ * Utils.waitNode("div", document).then( $div =>{
4107
4215
  * console.log($div); // div => HTMLDivELement
4108
4216
  * })
4109
4217
  */
@@ -4123,7 +4231,7 @@ class Utils {
4123
4231
  * Utils.waitNode(["div"]).then( ([$div]) =>{
4124
4232
  * console.log($div); // div => HTMLDivELement[]
4125
4233
  * })
4126
- * Utils.waitNode(["div"],document).then( ([$div]) =>{
4234
+ * Utils.waitNode(["div"], document).then( ([$div]) =>{
4127
4235
  * console.log($div); // div => HTMLDivELement[]
4128
4236
  * })
4129
4237
  */
@@ -4141,7 +4249,7 @@ class Utils {
4141
4249
  * @param parent 父元素,默认document
4142
4250
  * @param timeout 超时时间,默认0
4143
4251
  * @example
4144
- * Utils.waitNode("div",document,1000).then( $div =>{
4252
+ * Utils.waitNode("div", document, 1000).then( $div =>{
4145
4253
  * console.log($div); // $div => HTMLDivELement | null
4146
4254
  * })
4147
4255
  */
@@ -4161,7 +4269,7 @@ class Utils {
4161
4269
  * @param parent 父元素,默认document
4162
4270
  * @param timeout 超时时间,默认0
4163
4271
  * @example
4164
- * Utils.waitNode(["div"],document,1000).then( ([$div]) =>{
4272
+ * Utils.waitNode(["div"], document, 1000).then( ([$div]) =>{
4165
4273
  * console.log($div); // $div => HTMLDivELement[] | null
4166
4274
  * })
4167
4275
  */
@@ -4180,7 +4288,7 @@ class Utils {
4180
4288
  * @param selector CSS选择器
4181
4289
  * @param timeout 超时时间,默认0
4182
4290
  * @example
4183
- * Utils.waitNode("div",1000).then( $div =>{
4291
+ * Utils.waitNode("div", 1000).then( $div =>{
4184
4292
  * console.log($div); // $div => HTMLDivELement | null
4185
4293
  * })
4186
4294
  */
@@ -4197,7 +4305,7 @@ class Utils {
4197
4305
  * @param selectorList CSS选择器数组
4198
4306
  * @param timeout 超时时间,默认0
4199
4307
  * @example
4200
- * Utils.waitNode(["div"],1000).then( [$div] =>{
4308
+ * Utils.waitNode(["div"], 1000).then( [$div] =>{
4201
4309
  * console.log($div); // $div => HTMLDivELement[] | null
4202
4310
  * })
4203
4311
  */
@@ -4214,13 +4322,19 @@ class Utils {
4214
4322
  args = args.filter((arg) => arg !== void 0);
4215
4323
  let UtilsContext = this;
4216
4324
  // 选择器
4217
- let selector = args[0] as unknown as string | string[];
4325
+ let selector = args[0] as unknown as string | string[] | Function;
4218
4326
  // 父元素(监听的元素)
4219
4327
  let parent: Element = UtilsContext.windowApi.document as any as Element;
4220
4328
  // 超时时间
4221
4329
  let timeout = 0;
4222
- if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
4223
- throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]");
4330
+ if (
4331
+ typeof args[0] !== "string" &&
4332
+ !Array.isArray(args[0]) &&
4333
+ typeof args[0] !== "function"
4334
+ ) {
4335
+ throw new TypeError(
4336
+ "Utils.waitNode 第一个参数必须是string|string[]|Function"
4337
+ );
4224
4338
  }
4225
4339
  if (args.length === 1) {
4226
4340
  // 上面已做处理
@@ -4257,52 +4371,42 @@ class Utils {
4257
4371
  } else {
4258
4372
  throw new TypeError("Utils.waitNode 参数个数错误");
4259
4373
  }
4260
- return new Promise((resolve) => {
4261
- function getNode() {
4262
- if (Array.isArray(selector)) {
4263
- let result: T[] = [];
4264
- for (let index = 0; index < selector.length; index++) {
4265
- let node = parent.querySelector(selector[index]);
4266
- if (node) {
4267
- result.push(node as any);
4268
- }
4269
- }
4270
- if (result.length === selector.length) {
4271
- return result;
4272
- }
4273
- } else {
4274
- return parent.querySelector(selector);
4275
- }
4276
- }
4277
- var observer = UtilsContext.mutationObserver(parent, {
4278
- config: {
4279
- subtree: true,
4280
- childList: true,
4281
- attributes: true,
4282
- },
4283
- callback() {
4284
- let node = getNode();
4374
+ function getNode() {
4375
+ if (Array.isArray(selector)) {
4376
+ let result: T[] = [];
4377
+ for (let index = 0; index < selector.length; index++) {
4378
+ let node = parent.querySelector(selector[index]);
4285
4379
  if (node) {
4286
- // 取消观察器
4287
- if (typeof observer?.disconnect === "function") {
4288
- observer.disconnect();
4289
- }
4290
- resolve(node as any as T);
4291
- return;
4292
- }
4293
- },
4294
- immediate: true,
4295
- });
4296
- if (timeout > 0) {
4297
- setTimeout(() => {
4298
- // 取消观察器
4299
- if (typeof observer?.disconnect === "function") {
4300
- observer.disconnect();
4380
+ result.push(node as any);
4301
4381
  }
4302
- resolve(null);
4303
- }, timeout);
4382
+ }
4383
+ if (result.length === selector.length) {
4384
+ return result;
4385
+ }
4386
+ } else if (typeof selector === "function") {
4387
+ return selector();
4388
+ } else {
4389
+ return parent.querySelector(selector);
4304
4390
  }
4305
- });
4391
+ }
4392
+ return UtilsContext.wait(
4393
+ () => {
4394
+ let node = getNode();
4395
+ if (node) {
4396
+ return {
4397
+ success: true,
4398
+ data: node,
4399
+ };
4400
+ } else {
4401
+ return {
4402
+ success: false,
4403
+ data: node,
4404
+ };
4405
+ }
4406
+ },
4407
+ timeout,
4408
+ parent
4409
+ );
4306
4410
  }
4307
4411
  /**
4308
4412
  * 等待任意元素出现
@@ -4312,7 +4416,7 @@ class Utils {
4312
4416
  * Utils.waitAnyNode(["div","div"]).then( $div =>{
4313
4417
  * console.log($div); // $div => HTMLDivELement 这里是第一个
4314
4418
  * })
4315
- * Utils.waitAnyNode(["a","div"],document).then( $a =>{
4419
+ * Utils.waitAnyNode(["a","div"], document).then( $a =>{
4316
4420
  * console.log($a); // $a => HTMLAnchorElement 这里是第一个
4317
4421
  * })
4318
4422
  */
@@ -4330,7 +4434,7 @@ class Utils {
4330
4434
  * @param parent 父元素,默认document
4331
4435
  * @param timeout 超时时间,默认0
4332
4436
  * @example
4333
- * Utils.waitAnyNode(["div","div"],document,10000).then( $div =>{
4437
+ * Utils.waitAnyNode(["div","div"], document, 10000).then( $div =>{
4334
4438
  * console.log($div); // $div => HTMLDivELement | null
4335
4439
  * })
4336
4440
  */
@@ -4349,7 +4453,7 @@ class Utils {
4349
4453
  * @param selectorList CSS选择器数组
4350
4454
  * @param timeout 超时时间,默认0
4351
4455
  * @example
4352
- * Utils.waitAnyNode(["div","div"],10000).then( $div =>{
4456
+ * Utils.waitAnyNode(["div","div"], 10000).then( $div =>{
4353
4457
  * console.log($div); // $div => HTMLDivELement | null
4354
4458
  * })
4355
4459
  */
@@ -4414,7 +4518,6 @@ class Utils {
4414
4518
  });
4415
4519
  return Promise.any(promiseList);
4416
4520
  }
4417
-
4418
4521
  /**
4419
4522
  * 等待元素数组出现
4420
4523
  * @param selector CSS选择器
@@ -4423,7 +4526,7 @@ class Utils {
4423
4526
  * Utils.waitNodeList("div").then( $result =>{
4424
4527
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4425
4528
  * })
4426
- * Utils.waitNodeList("div",document).then( $result =>{
4529
+ * Utils.waitNodeList("div", document).then( $result =>{
4427
4530
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4428
4531
  * })
4429
4532
  */
@@ -4443,7 +4546,7 @@ class Utils {
4443
4546
  * Utils.waitNodeList(["div"]).then( $result =>{
4444
4547
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
4445
4548
  * })
4446
- * Utils.waitNodeList(["div"],document).then( $result =>{
4549
+ * Utils.waitNodeList(["div"], document).then( $result =>{
4447
4550
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
4448
4551
  * })
4449
4552
  */
@@ -4461,7 +4564,7 @@ class Utils {
4461
4564
  * @param parent 监听的父元素
4462
4565
  * @param timeout 超时时间,默认0
4463
4566
  * @example
4464
- * Utils.waitNodeList("div",document,10000).then( $result =>{
4567
+ * Utils.waitNodeList("div", document, 10000).then( $result =>{
4465
4568
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4466
4569
  * })
4467
4570
  */
@@ -4481,7 +4584,7 @@ class Utils {
4481
4584
  * @param parent 监听的父元素
4482
4585
  * @param timeout 超时时间,默认0
4483
4586
  * @example
4484
- * Utils.waitNodeList(["div"],document,10000).then( $result =>{
4587
+ * Utils.waitNodeList(["div"], document, 10000).then( $result =>{
4485
4588
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
4486
4589
  * })
4487
4590
  */
@@ -4500,7 +4603,7 @@ class Utils {
4500
4603
  * @param selector CSS选择器数组
4501
4604
  * @param timeout 超时时间,默认0
4502
4605
  * @example
4503
- * Utils.waitNodeList("div",10000).then( $result =>{
4606
+ * Utils.waitNodeList("div", 10000).then( $result =>{
4504
4607
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4505
4608
  * })
4506
4609
  */
@@ -4517,7 +4620,7 @@ class Utils {
4517
4620
  * @param selectorList CSS选择器数组
4518
4621
  * @param timeout 超时时间,默认0
4519
4622
  * @example
4520
- * Utils.waitNodeList(["div"],10000).then( $result =>{
4623
+ * Utils.waitNodeList(["div"], 10000).then( $result =>{
4521
4624
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
4522
4625
  * })
4523
4626
  */
@@ -4579,57 +4682,45 @@ class Utils {
4579
4682
  } else {
4580
4683
  throw new TypeError("Utils.waitNodeList 参数个数错误");
4581
4684
  }
4582
- return new Promise((resolve) => {
4583
- function getNodeList() {
4584
- if (Array.isArray(selector)) {
4585
- let result: T[] = [];
4586
- for (let index = 0; index < selector.length; index++) {
4587
- let nodeList = (parent as Element).querySelectorAll(
4588
- selector[index]
4589
- ) as T;
4590
- if (nodeList.length) {
4591
- result.push(nodeList);
4592
- }
4593
- }
4594
- if (result.length === selector.length) {
4595
- return result;
4596
- }
4597
- } else {
4598
- let nodeList = (parent as Element).querySelectorAll(selector) as T;
4685
+ function getNodeList() {
4686
+ if (Array.isArray(selector)) {
4687
+ let result: T[] = [];
4688
+ for (let index = 0; index < selector.length; index++) {
4689
+ let nodeList = (parent as Element).querySelectorAll(
4690
+ selector[index]
4691
+ ) as T;
4599
4692
  if (nodeList.length) {
4600
- return nodeList;
4693
+ result.push(nodeList);
4601
4694
  }
4602
4695
  }
4696
+ if (result.length === selector.length) {
4697
+ return result;
4698
+ }
4699
+ } else {
4700
+ let nodeList = (parent as Element).querySelectorAll(selector) as T;
4701
+ if (nodeList.length) {
4702
+ return nodeList;
4703
+ }
4603
4704
  }
4604
- var observer = UtilsContext.mutationObserver(parent, {
4605
- config: {
4606
- subtree: true,
4607
- childList: true,
4608
- attributes: true,
4609
- },
4610
- callback() {
4611
- let node = getNodeList();
4612
- if (node) {
4613
- // 取消观察器
4614
- try {
4615
- observer.disconnect();
4616
- } catch (error) {}
4617
- resolve(node as T);
4618
- return;
4619
- }
4620
- },
4621
- immediate: true,
4622
- });
4623
- if (timeout > 0) {
4624
- setTimeout(() => {
4625
- // 取消观察器
4626
- if (typeof observer?.disconnect === "function") {
4627
- observer.disconnect();
4628
- }
4629
- resolve(null);
4630
- }, timeout);
4631
- }
4632
- });
4705
+ }
4706
+ return UtilsContext.wait<any>(
4707
+ () => {
4708
+ let node = getNodeList();
4709
+ if (node) {
4710
+ return {
4711
+ success: true,
4712
+ data: node,
4713
+ };
4714
+ } else {
4715
+ return {
4716
+ success: false,
4717
+ data: node,
4718
+ };
4719
+ }
4720
+ },
4721
+ timeout,
4722
+ parent
4723
+ );
4633
4724
  }
4634
4725
  /**
4635
4726
  * 等待任意元素数组出现
@@ -4639,7 +4730,7 @@ class Utils {
4639
4730
  * Utils.waitAnyNodeList(["div","a"]).then( $result =>{
4640
4731
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4641
4732
  * })
4642
- * Utils.waitAnyNodeList(["div","a"],document).then( $result =>{
4733
+ * Utils.waitAnyNodeList(["div","a"], document).then( $result =>{
4643
4734
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4644
4735
  * })
4645
4736
  */
@@ -4657,7 +4748,7 @@ class Utils {
4657
4748
  * @param parent 父元素,默认document
4658
4749
  * @param timeout 超时时间,默认0
4659
4750
  * @example
4660
- * Utils.waitAnyNodeList(["div","a"],document,10000).then( $result =>{
4751
+ * Utils.waitAnyNodeList(["div","a"], document, 10000).then( $result =>{
4661
4752
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4662
4753
  * })
4663
4754
  */
@@ -4676,7 +4767,7 @@ class Utils {
4676
4767
  * @param selectorList CSS选择器数组
4677
4768
  * @param timeout 超时时间,默认0
4678
4769
  * @example
4679
- * Utils.waitAnyNodeList(["div","div"],10000).then( $result =>{
4770
+ * Utils.waitAnyNodeList(["div","div"], 10000).then( $result =>{
4680
4771
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4681
4772
  * })
4682
4773
  */
@@ -5116,6 +5207,7 @@ class Utils {
5116
5207
  * > "测试"
5117
5208
  */
5118
5209
  Vue = Vue;
5210
+ ModuleRaid = ModuleRaid;
5119
5211
  }
5120
5212
 
5121
5213
  let utils = new Utils();
@@ -25,3 +25,4 @@ export declare interface AnyObject {
25
25
  export type PartialKeys<T, K extends keyof T> = {
26
26
  [P in K]?: T[P];
27
27
  };
28
+ export type Values<T> = T[keyof T];