assistsx-js 0.0.1353 → 0.0.2001

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.
@@ -0,0 +1,719 @@
1
+ /**
2
+ * AssistsX 类
3
+ * 提供与移动应用程序界面交互的工具类,包括节点查找、手势操作、屏幕操作等功能
4
+ */
5
+ import { Node } from "./Node";
6
+ import { CallMethod } from "./CallMethod";
7
+ import { CallResponse } from "./CallResponse";
8
+ import { Bounds } from "./Bounds";
9
+ import { generateUUID } from "./Utils";
10
+ import { AssistsX, callbacks, WebFloatingWindowOptions } from "./AssistsX";
11
+
12
+ export class AssistsXAsync {
13
+ /**
14
+ * 执行异步调用
15
+ * @param method 方法名
16
+ * @param args 参数对象
17
+ * @returns Promise<调用响应>
18
+ */
19
+ private static async asyncCall(
20
+ method: string,
21
+ { args, node, nodes }: { args?: any; node?: Node; nodes?: Node[] } = {}
22
+ ): Promise<CallResponse> {
23
+ const uuid = generateUUID();
24
+ const params = {
25
+ method,
26
+ arguments: args ? args : undefined,
27
+ node: node ? node : undefined,
28
+ nodes: nodes ? nodes : undefined,
29
+ callbackId: uuid,
30
+ };
31
+ const promise = new Promise((resolve) => {
32
+ callbacks[uuid] = (data: any) => {
33
+ resolve(data);
34
+ };
35
+ setTimeout(() => {
36
+ resolve(new CallResponse(0, null, uuid));
37
+ }, 10000);
38
+ });
39
+ const result = window.assistsxAsync.call(JSON.stringify(params));
40
+ const promiseResult = await promise;
41
+ if (typeof promiseResult === "string") {
42
+ const responseData = JSON.parse(promiseResult);
43
+ const response = new CallResponse(
44
+ responseData.code,
45
+ responseData.data,
46
+ responseData.callbackId
47
+ );
48
+ return response;
49
+ }
50
+ throw new Error("Call failed");
51
+ }
52
+
53
+ /**
54
+ * 设置悬浮窗标志
55
+ * @param flags 标志
56
+ * @returns 是否设置成功
57
+ */
58
+ public static async setOverlayFlags(flags: number): Promise<boolean> {
59
+ const response = await this.asyncCall(CallMethod.setOverlayFlags, {
60
+ args: { flags: flags },
61
+ });
62
+ return response.getDataOrDefault(false);
63
+ }
64
+ /**
65
+ * 设置悬浮窗标志
66
+ * @param flags 标志
67
+ * @returns 是否设置成功
68
+ */
69
+ public static async setOverlayFlagList(flags: number[]): Promise<boolean> {
70
+ const response = await this.asyncCall(CallMethod.setOverlayFlags, {
71
+ args: { flags: flags },
72
+ });
73
+ return response.getDataOrDefault(false);
74
+ }
75
+ /**
76
+ * 获取所有符合条件的节点
77
+ * @param filterClass 类名过滤
78
+ * @param filterViewId 视图ID过滤
79
+ * @param filterDes 描述过滤
80
+ * @param filterText 文本过滤
81
+ * @returns 节点数组
82
+ */
83
+ public static async getAllNodes({
84
+ filterClass,
85
+ filterViewId,
86
+ filterDes,
87
+ filterText,
88
+ }: {
89
+ filterClass?: string;
90
+ filterViewId?: string;
91
+ filterDes?: string;
92
+ filterText?: string;
93
+ } = {}): Promise<Node[]> {
94
+ const response = await this.asyncCall(CallMethod.getAllNodes, {
95
+ args: { filterClass, filterViewId, filterDes, filterText },
96
+ });
97
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
98
+ }
99
+
100
+ /**
101
+ * 设置节点文本
102
+ * @param node 目标节点
103
+ * @param text 要设置的文本
104
+ * @returns 是否设置成功
105
+ */
106
+ public static async setNodeText(node: Node, text: string): Promise<boolean> {
107
+ const response = await this.asyncCall(CallMethod.setNodeText, {
108
+ args: { text },
109
+ node,
110
+ });
111
+ return response.getDataOrDefault(false);
112
+ }
113
+
114
+ /**
115
+ * 对指定节点进行截图
116
+ * @param nodes 要截图的节点数组
117
+ * @param overlayHiddenScreenshotDelayMillis 截图延迟时间(毫秒)
118
+ * @returns 截图路径数组
119
+ */
120
+ public static async takeScreenshotNodes(
121
+ nodes: Node[],
122
+ overlayHiddenScreenshotDelayMillis: number = 250
123
+ ): Promise<string[]> {
124
+ const response = await this.asyncCall(CallMethod.takeScreenshot, {
125
+ nodes,
126
+ args: { overlayHiddenScreenshotDelayMillis },
127
+ });
128
+ const data = response.getDataOrDefault("");
129
+ return data.images;
130
+ }
131
+ public static async scanQR(): Promise<string> {
132
+ const response = await this.asyncCall(CallMethod.scanQR);
133
+ const data = response.getDataOrDefault({ value: "" });
134
+ return data.value;
135
+ }
136
+ public static async loadWebViewOverlay(
137
+ url: string,
138
+ options: WebFloatingWindowOptions = {}
139
+ ): Promise<any> {
140
+ const {
141
+ initialWidth,
142
+ initialHeight,
143
+ minWidth,
144
+ minHeight,
145
+ maxWidth,
146
+ maxHeight,
147
+ initialCenter,
148
+ } = options;
149
+ const response = await this.asyncCall(CallMethod.loadWebViewOverlay, {
150
+ args: {
151
+ url,
152
+ initialWidth,
153
+ initialHeight,
154
+ minWidth,
155
+ minHeight,
156
+ maxWidth,
157
+ maxHeight,
158
+ initialCenter,
159
+ },
160
+ });
161
+ const data = response.getDataOrDefault({});
162
+ return data;
163
+ }
164
+
165
+ /**
166
+ * 点击节点
167
+ * @param node 要点击的节点
168
+ * @returns 是否点击成功
169
+ */
170
+ public static async click(node: Node): Promise<boolean> {
171
+ const response = await this.asyncCall(CallMethod.click, { node });
172
+ return response.getDataOrDefault(false);
173
+ }
174
+
175
+ /**
176
+ * 长按节点
177
+ * @param node 要长按的节点
178
+ * @returns 是否长按成功
179
+ */
180
+ public static async longClick(node: Node): Promise<boolean> {
181
+ const response = await this.asyncCall(CallMethod.longClick, { node });
182
+ return response.getDataOrDefault(false);
183
+ }
184
+
185
+ /**
186
+ * 启动应用
187
+ * @param packageName 应用包名
188
+ * @returns 是否启动成功
189
+ */
190
+ public static async launchApp(packageName: string): Promise<boolean> {
191
+ const response = await this.asyncCall(CallMethod.launchApp, {
192
+ args: { packageName },
193
+ });
194
+ return response.getDataOrDefault(false);
195
+ }
196
+
197
+ /**
198
+ * 获取当前应用包名
199
+ * @returns 包名
200
+ */
201
+ public static async getPackageName(): Promise<string> {
202
+ const response = await this.asyncCall(CallMethod.getPackageName);
203
+ return response.getDataOrDefault("");
204
+ }
205
+
206
+ /**
207
+ * 显示悬浮提示
208
+ * @param text 提示文本
209
+ * @param delay 显示时长(毫秒)
210
+ * @returns 是否显示成功
211
+ */
212
+ public static async overlayToast(
213
+ text: string,
214
+ delay: number = 2000
215
+ ): Promise<boolean> {
216
+ const response = await this.asyncCall(CallMethod.overlayToast, {
217
+ args: { text, delay },
218
+ });
219
+ return response.getDataOrDefault(false);
220
+ }
221
+
222
+ /**
223
+ * 通过ID查找节点
224
+ * @param id 节点ID
225
+ * @param filterClass 类名过滤
226
+ * @param filterText 文本过滤
227
+ * @param filterDes 描述过滤
228
+ * @param node 父节点范围
229
+ * @returns 节点数组
230
+ */
231
+ public static async findById(
232
+ id: string,
233
+ {
234
+ filterClass,
235
+ filterText,
236
+ filterDes,
237
+ node,
238
+ }: {
239
+ filterClass?: string;
240
+ filterText?: string;
241
+ filterDes?: string;
242
+ node?: Node;
243
+ } = {}
244
+ ): Promise<Node[]> {
245
+ const response = await this.asyncCall(CallMethod.findById, {
246
+ args: { id, filterClass, filterText, filterDes },
247
+ node,
248
+ });
249
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
250
+ }
251
+
252
+ /**
253
+ * 通过文本查找节点
254
+ * @param text 要查找的文本
255
+ * @param filterClass 类名过滤
256
+ * @param filterViewId 视图ID过滤
257
+ * @param filterDes 描述过滤
258
+ * @param node 父节点范围
259
+ * @returns 节点数组
260
+ */
261
+ public static async findByText(
262
+ text: string,
263
+ {
264
+ filterClass,
265
+ filterViewId,
266
+ filterDes,
267
+ node,
268
+ }: {
269
+ filterClass?: string;
270
+ filterViewId?: string;
271
+ filterDes?: string;
272
+ node?: Node;
273
+ } = {}
274
+ ): Promise<Node[]> {
275
+ const response = await this.asyncCall(CallMethod.findByText, {
276
+ args: { text, filterClass, filterViewId, filterDes },
277
+ node,
278
+ });
279
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
280
+ }
281
+
282
+ /**
283
+ * 通过标签查找节点
284
+ * @param className 类名
285
+ * @param filterText 文本过滤
286
+ * @param filterViewId 视图ID过滤
287
+ * @param filterDes 描述过滤
288
+ * @param node 父节点范围
289
+ * @returns 节点数组
290
+ */
291
+ public static async findByTags(
292
+ className: string,
293
+ {
294
+ filterText,
295
+ filterViewId,
296
+ filterDes,
297
+ node,
298
+ }: {
299
+ filterText?: string;
300
+ filterViewId?: string;
301
+ filterDes?: string;
302
+ node?: Node;
303
+ } = {}
304
+ ): Promise<Node[]> {
305
+ const response = await this.asyncCall(CallMethod.findByTags, {
306
+ args: { className, filterText, filterViewId, filterDes },
307
+ node,
308
+ });
309
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
310
+ }
311
+
312
+ /**
313
+ * 查找所有匹配文本的节点
314
+ * @param text 要查找的文本
315
+ * @returns 节点数组
316
+ */
317
+ public static async findByTextAllMatch(text: string): Promise<Node[]> {
318
+ const response = await this.asyncCall(CallMethod.findByTextAllMatch, {
319
+ args: { text },
320
+ });
321
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
322
+ }
323
+
324
+ /**
325
+ * 检查是否包含指定文本
326
+ * @param text 要检查的文本
327
+ * @returns 是否包含
328
+ */
329
+ public static async containsText(text: string): Promise<boolean> {
330
+ const response = await this.asyncCall(CallMethod.containsText, {
331
+ args: { text },
332
+ });
333
+ return response.getDataOrDefault(false);
334
+ }
335
+
336
+ /**
337
+ * 获取所有文本
338
+ * @returns 文本数组
339
+ */
340
+ public static async getAllText(): Promise<string[]> {
341
+ const response = await this.asyncCall(CallMethod.getAllText);
342
+ return response.getDataOrDefault("[]");
343
+ }
344
+
345
+ /**
346
+ * 查找第一个匹配标签的父节点
347
+ * @param className 类名
348
+ * @returns 父节点
349
+ */
350
+ public static async findFirstParentByTags(className: string): Promise<Node> {
351
+ const response = await this.asyncCall(CallMethod.findFirstParentByTags, {
352
+ args: { className },
353
+ });
354
+ return Node.create(response.getDataOrDefault("{}"));
355
+ }
356
+
357
+ /**
358
+ * 获取节点的所有子节点
359
+ * @param node 父节点
360
+ * @returns 子节点数组
361
+ */
362
+ public static async getNodes(node: Node): Promise<Node[]> {
363
+ const response = await this.asyncCall(CallMethod.getNodes, { node });
364
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
365
+ }
366
+
367
+ /**
368
+ * 获取节点的直接子节点
369
+ * @param node 父节点
370
+ * @returns 子节点数组
371
+ */
372
+ public static async getChildren(node: Node): Promise<Node[]> {
373
+ const response = await this.asyncCall(CallMethod.getChildren, { node });
374
+ return Node.fromJSONArray(response.getDataOrDefault([]));
375
+ }
376
+
377
+ /**
378
+ * 查找第一个可点击的父节点
379
+ * @param node 起始节点
380
+ * @returns 可点击的父节点
381
+ */
382
+ public static async findFirstParentClickable(node: Node): Promise<Node> {
383
+ const response = await this.asyncCall(CallMethod.findFirstParentClickable, {
384
+ node,
385
+ });
386
+ return Node.create(response.getDataOrDefault("{}"));
387
+ }
388
+
389
+ /**
390
+ * 获取节点在屏幕中的边界
391
+ * @param node 目标节点
392
+ * @returns 边界对象
393
+ */
394
+ public static async getBoundsInScreen(node: Node): Promise<Bounds> {
395
+ const response = await this.asyncCall(CallMethod.getBoundsInScreen, {
396
+ node,
397
+ });
398
+ return Bounds.fromData(response.getDataOrDefault({}));
399
+ }
400
+
401
+ /**
402
+ * 检查节点是否可见
403
+ * @param node 目标节点
404
+ * @param compareNode 比较节点
405
+ * @param isFullyByCompareNode 是否完全可见
406
+ * @returns 是否可见
407
+ */
408
+ public static async isVisible(
409
+ node: Node,
410
+ {
411
+ compareNode,
412
+ isFullyByCompareNode,
413
+ }: { compareNode?: Node; isFullyByCompareNode?: boolean } = {}
414
+ ): Promise<boolean> {
415
+ const response = await this.asyncCall(CallMethod.isVisible, {
416
+ node,
417
+ args: { compareNode, isFullyByCompareNode },
418
+ });
419
+ return response.getDataOrDefault(false);
420
+ }
421
+
422
+ /**
423
+ * 执行点击手势
424
+ * @param x 横坐标
425
+ * @param y 纵坐标
426
+ * @param duration 持续时间
427
+ * @returns 是否成功
428
+ */
429
+ public static async clickByGesture(
430
+ x: number,
431
+ y: number,
432
+ duration: number
433
+ ): Promise<boolean> {
434
+ const response = await this.asyncCall(CallMethod.clickByGesture, {
435
+ args: { x, y, duration },
436
+ });
437
+ return response.getDataOrDefault(false);
438
+ }
439
+
440
+ /**
441
+ * 返回操作
442
+ * @returns 是否成功
443
+ */
444
+ public static async back(): Promise<boolean> {
445
+ const response = await this.asyncCall(CallMethod.back);
446
+ return response.getDataOrDefault(false);
447
+ }
448
+
449
+ /**
450
+ * 回到主页
451
+ * @returns 是否成功
452
+ */
453
+ public static async home(): Promise<boolean> {
454
+ const response = await this.asyncCall(CallMethod.home);
455
+ return response.getDataOrDefault(false);
456
+ }
457
+
458
+ /**
459
+ * 打开通知栏
460
+ * @returns 是否成功
461
+ */
462
+ public static async notifications(): Promise<boolean> {
463
+ const response = await this.asyncCall(CallMethod.notifications);
464
+ return response.getDataOrDefault(false);
465
+ }
466
+
467
+ /**
468
+ * 显示最近应用
469
+ * @returns 是否成功
470
+ */
471
+ public static async recentApps(): Promise<boolean> {
472
+ const response = await this.asyncCall(CallMethod.recentApps);
473
+ return response.getDataOrDefault(false);
474
+ }
475
+
476
+ /**
477
+ * 在节点中粘贴文本
478
+ * @param node 目标节点
479
+ * @param text 要粘贴的文本
480
+ * @returns 是否成功
481
+ */
482
+ public static async paste(node: Node, text: string): Promise<boolean> {
483
+ const response = await this.asyncCall(CallMethod.paste, {
484
+ args: { text },
485
+ node,
486
+ });
487
+ return response.getDataOrDefault(false);
488
+ }
489
+ public static async focus(node: Node): Promise<boolean> {
490
+ const response = await this.asyncCall(CallMethod.focus, { node });
491
+ return response.getDataOrDefault(false);
492
+ }
493
+
494
+ /**
495
+ * 选择文本
496
+ * @param node 目标节点
497
+ * @param selectionStart 选择起始位置
498
+ * @param selectionEnd 选择结束位置
499
+ * @returns 是否成功
500
+ */
501
+ public static async selectionText(
502
+ node: Node,
503
+ selectionStart: number,
504
+ selectionEnd: number
505
+ ): Promise<boolean> {
506
+ const response = await this.asyncCall(CallMethod.selectionText, {
507
+ args: { selectionStart, selectionEnd },
508
+ node,
509
+ });
510
+ return response.getDataOrDefault(false);
511
+ }
512
+
513
+ /**
514
+ * 向前滚动
515
+ * @param node 可滚动节点
516
+ * @returns 是否成功
517
+ */
518
+ public static async scrollForward(node: Node): Promise<boolean> {
519
+ const response = await this.asyncCall(CallMethod.scrollForward, {
520
+ node,
521
+ });
522
+ return response.getDataOrDefault(false);
523
+ }
524
+
525
+ /**
526
+ * 向后滚动
527
+ * @param node 可滚动节点
528
+ * @returns 是否成功
529
+ */
530
+ public static async scrollBackward(node: Node): Promise<boolean> {
531
+ const response = await this.asyncCall(CallMethod.scrollBackward, {
532
+ node,
533
+ });
534
+ return response.getDataOrDefault(false);
535
+ }
536
+
537
+ /**
538
+ * 对节点执行点击手势
539
+ * @param node 目标节点
540
+ * @param offsetX X轴偏移
541
+ * @param offsetY Y轴偏移
542
+ * @param switchWindowIntervalDelay 窗口切换延迟
543
+ * @param clickDuration 点击持续时间
544
+ * @returns 是否成功
545
+ */
546
+ public static async clickNodeByGesture(
547
+ node: Node,
548
+ {
549
+ offsetX,
550
+ offsetY,
551
+ switchWindowIntervalDelay,
552
+ clickDuration,
553
+ }: {
554
+ offsetX?: number;
555
+ offsetY?: number;
556
+ switchWindowIntervalDelay?: number;
557
+ clickDuration?: number;
558
+ } = {}
559
+ ): Promise<boolean> {
560
+ const response = await this.asyncCall(CallMethod.clickNodeByGesture, {
561
+ node,
562
+ args: { offsetX, offsetY, switchWindowIntervalDelay, clickDuration },
563
+ });
564
+ return response.getDataOrDefault(false);
565
+ }
566
+
567
+ /**
568
+ * 对节点执行双击手势
569
+ * @param node 目标节点
570
+ * @param offsetX X轴偏移
571
+ * @param offsetY Y轴偏移
572
+ * @param switchWindowIntervalDelay 窗口切换延迟
573
+ * @param clickDuration 点击持续时间
574
+ * @param clickInterval 点击间隔
575
+ * @returns 是否成功
576
+ */
577
+ public static async doubleClickNodeByGesture(
578
+ node: Node,
579
+ {
580
+ offsetX,
581
+ offsetY,
582
+ switchWindowIntervalDelay,
583
+ clickDuration,
584
+ clickInterval,
585
+ }: {
586
+ offsetX?: number;
587
+ offsetY?: number;
588
+ switchWindowIntervalDelay?: number;
589
+ clickDuration?: number;
590
+ clickInterval?: number;
591
+ } = {}
592
+ ): Promise<boolean> {
593
+ const response = await this.asyncCall(CallMethod.doubleClickNodeByGesture, {
594
+ node,
595
+ args: {
596
+ offsetX,
597
+ offsetY,
598
+ switchWindowIntervalDelay,
599
+ clickDuration,
600
+ clickInterval,
601
+ },
602
+ });
603
+ return response.getDataOrDefault(false);
604
+ }
605
+ /**
606
+ * 执行线型手势
607
+ * @param startPoint
608
+ * @param endPoint
609
+ * @param param2
610
+ * @returns
611
+ */
612
+ public static async performLinearGesture(
613
+ startPoint: { x: number; y: number },
614
+ endPoint: { x: number; y: number },
615
+ { duration }: { duration?: number } = {}
616
+ ): Promise<boolean> {
617
+ const response = await this.asyncCall(CallMethod.performLinearGesture, {
618
+ args: { startPoint, endPoint, duration },
619
+ });
620
+ return response.getDataOrDefault(false);
621
+ }
622
+ public static async longPressNodeByGestureAutoPaste(
623
+ node: Node,
624
+ text: string,
625
+ {
626
+ matchedPackageName,
627
+ matchedText,
628
+ timeoutMillis,
629
+ longPressDuration,
630
+ }: {
631
+ matchedPackageName?: string;
632
+ matchedText?: string;
633
+ timeoutMillis?: number;
634
+ longPressDuration?: number;
635
+ } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
636
+ ): Promise<boolean> {
637
+ const response = await this.asyncCall(
638
+ CallMethod.longPressGestureAutoPaste,
639
+ {
640
+ node,
641
+ args: {
642
+ text,
643
+ matchedPackageName,
644
+ matchedText,
645
+ timeoutMillis,
646
+ longPressDuration,
647
+ },
648
+ }
649
+ );
650
+ return response.getDataOrDefault(false);
651
+ }
652
+
653
+ public static async longPressGestureAutoPaste(
654
+ point: { x: number; y: number },
655
+ text: string,
656
+ {
657
+ matchedPackageName,
658
+ matchedText,
659
+ timeoutMillis,
660
+ longPressDuration,
661
+ }: {
662
+ matchedPackageName?: string;
663
+ matchedText?: string;
664
+ timeoutMillis?: number;
665
+ longPressDuration?: number;
666
+ } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
667
+ ): Promise<boolean> {
668
+ const response = await this.asyncCall(
669
+ CallMethod.longPressGestureAutoPaste,
670
+ {
671
+ args: {
672
+ point,
673
+ text,
674
+ matchedPackageName,
675
+ matchedText,
676
+ timeoutMillis,
677
+ longPressDuration,
678
+ },
679
+ }
680
+ );
681
+ return response.getDataOrDefault(false);
682
+ }
683
+ public static async getAppInfo(packageName: string): Promise<any> {
684
+ const response = await this.asyncCall(CallMethod.getAppInfo, {
685
+ args: { packageName },
686
+ });
687
+ return response.getDataOrDefault({});
688
+ }
689
+ public static async getUniqueDeviceId(): Promise<any> {
690
+ const response = await this.asyncCall(CallMethod.getUniqueDeviceId);
691
+ return response.getDataOrDefault("");
692
+ }
693
+ public static async getAndroidID(): Promise<any> {
694
+ const response = await this.asyncCall(CallMethod.getAndroidID);
695
+ return response.getDataOrDefault("");
696
+ }
697
+ public static async getMacAddress(): Promise<any> {
698
+ const response = await this.asyncCall(CallMethod.getMacAddress);
699
+ return response.getDataOrDefault({});
700
+ }
701
+
702
+ /**
703
+ * 获取屏幕尺寸
704
+ * @returns 屏幕尺寸对象
705
+ */
706
+ public static async getScreenSize(): Promise<any> {
707
+ const response = await this.asyncCall(CallMethod.getScreenSize);
708
+ return response.getDataOrDefault("{}");
709
+ }
710
+
711
+ /**
712
+ * 获取应用窗口尺寸
713
+ * @returns 应用窗口尺寸对象
714
+ */
715
+ public static async getAppScreenSize(): Promise<any> {
716
+ const response = await this.asyncCall(CallMethod.getAppScreenSize);
717
+ return response.getDataOrDefault("{}");
718
+ }
719
+ }