assistsx-js 0.0.1353 → 0.0.2002

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,365 @@
1
+ /**
2
+ * 步骤执行控制类
3
+ * 用于管理和执行自动化步骤,提供步骤的生命周期管理、状态控制和界面操作功能
4
+ */
5
+ import { AssistsX } from "./AssistsX";
6
+ import { Node } from "./Node";
7
+ import { CallMethod } from "./CallMethod";
8
+ import { useStepStore } from "./StepStateStore";
9
+ import { generateUUID } from "./Utils";
10
+ import { StepError } from "./StepError";
11
+ import { AssistsXAsync } from "./AssistsXAsync";
12
+ import { Step } from "./Step";
13
+
14
+ export class StepAsync {
15
+ private step: Step;
16
+
17
+ /**
18
+ * 构造函数
19
+ * @param step Step实例
20
+ */
21
+ constructor(step: Step) {
22
+ this.step = step;
23
+ }
24
+
25
+ /**
26
+ * 对单个节点进行截图
27
+ * @param node 目标节点
28
+ * @param overlayHiddenScreenshotDelayMillis 截图延迟时间(毫秒)
29
+ * @returns 截图路径
30
+ */
31
+ public async takeScreenshotByNode(
32
+ node: Node,
33
+ overlayHiddenScreenshotDelayMillis: number = 250
34
+ ): Promise<string> {
35
+ Step.assert(this.step.stepId);
36
+ const result = await AssistsXAsync.takeScreenshotNodes(
37
+ [node],
38
+ overlayHiddenScreenshotDelayMillis
39
+ );
40
+ Step.assert(this.step.stepId);
41
+ return result[0];
42
+ }
43
+
44
+ /**
45
+ * 对多个节点进行截图
46
+ * @param nodes 目标节点数组
47
+ * @param overlayHiddenScreenshotDelayMillis 截图延迟时间(毫秒)
48
+ * @returns 截图路径数组
49
+ */
50
+ public async takeScreenshotNodes(
51
+ nodes: Node[],
52
+ overlayHiddenScreenshotDelayMillis: number = 250
53
+ ): Promise<string[]> {
54
+ Step.assert(this.step.stepId);
55
+ const result = await AssistsXAsync.takeScreenshotNodes(
56
+ nodes,
57
+ overlayHiddenScreenshotDelayMillis
58
+ );
59
+ Step.assert(this.step.stepId);
60
+ return result;
61
+ }
62
+
63
+ /**
64
+ * 获取所有符合条件的节点
65
+ * @param filterClass 类名过滤
66
+ * @param filterViewId 视图ID过滤
67
+ * @param filterDes 描述过滤
68
+ * @param filterText 文本过滤
69
+ * @returns 节点数组
70
+ */
71
+ public async getAllNodes({
72
+ filterClass,
73
+ filterViewId,
74
+ filterDes,
75
+ filterText,
76
+ }: {
77
+ filterClass?: string;
78
+ filterViewId?: string;
79
+ filterDes?: string;
80
+ filterText?: string;
81
+ } = {}): Promise<Node[]> {
82
+ Step.assert(this.step.stepId);
83
+ const nodes = await AssistsXAsync.getAllNodes({
84
+ filterClass,
85
+ filterViewId,
86
+ filterDes,
87
+ filterText,
88
+ });
89
+ Step.assert(this.step.stepId);
90
+ Step.assignIdsToNodes(nodes, this.step.stepId);
91
+ return nodes;
92
+ }
93
+
94
+ /**
95
+ * 启动应用
96
+ * @param packageName 应用包名
97
+ * @returns 是否启动成功
98
+ */
99
+ public async launchApp(packageName: string): Promise<boolean> {
100
+ Step.assert(this.step.stepId);
101
+ const result = await AssistsXAsync.launchApp(packageName);
102
+ Step.assert(this.step.stepId);
103
+ return result;
104
+ }
105
+
106
+ /**
107
+ * 获取当前应用包名
108
+ * @returns 包名
109
+ */
110
+ public async getPackageName(): Promise<string> {
111
+ Step.assert(this.step.stepId);
112
+ const result = await AssistsXAsync.getPackageName();
113
+ Step.assert(this.step.stepId);
114
+ return result;
115
+ }
116
+
117
+ /**
118
+ * 通过ID查找节点
119
+ * @param id 节点ID
120
+ * @param filterClass 类名过滤
121
+ * @param filterText 文本过滤
122
+ * @param filterDes 描述过滤
123
+ * @returns 节点数组
124
+ */
125
+ public async findById(
126
+ id: string,
127
+ {
128
+ filterClass,
129
+ filterText,
130
+ filterDes,
131
+ }: { filterClass?: string; filterText?: string; filterDes?: string } = {}
132
+ ): Promise<Node[]> {
133
+ Step.assert(this.step.stepId);
134
+ const nodes = await AssistsXAsync.findById(id, {
135
+ filterClass,
136
+ filterText,
137
+ filterDes,
138
+ });
139
+ Step.assert(this.step.stepId);
140
+ Step.assignIdsToNodes(nodes, this.step.stepId);
141
+ return nodes;
142
+ }
143
+
144
+ /**
145
+ * 通过文本查找节点
146
+ * @param text 要查找的文本
147
+ * @param filterClass 类名过滤
148
+ * @param filterViewId 视图ID过滤
149
+ * @param filterDes 描述过滤
150
+ * @returns 节点数组
151
+ */
152
+ public async findByText(
153
+ text: string,
154
+ {
155
+ filterClass,
156
+ filterViewId,
157
+ filterDes,
158
+ }: { filterClass?: string; filterViewId?: string; filterDes?: string } = {}
159
+ ): Promise<Node[]> {
160
+ Step.assert(this.step.stepId);
161
+ const nodes = await AssistsXAsync.findByText(text, {
162
+ filterClass,
163
+ filterViewId,
164
+ filterDes,
165
+ });
166
+ Step.assert(this.step.stepId);
167
+ Step.assignIdsToNodes(nodes, this.step.stepId);
168
+ return nodes;
169
+ }
170
+
171
+ /**
172
+ * 通过标签查找节点
173
+ * @param className 类名
174
+ * @param filterText 文本过滤
175
+ * @param filterViewId 视图ID过滤
176
+ * @param filterDes 描述过滤
177
+ * @returns 节点数组
178
+ */
179
+ public async findByTags(
180
+ className: string,
181
+ {
182
+ filterText,
183
+ filterViewId,
184
+ filterDes,
185
+ }: { filterText?: string; filterViewId?: string; filterDes?: string } = {}
186
+ ): Promise<Node[]> {
187
+ Step.assert(this.step.stepId);
188
+ const nodes = await AssistsXAsync.findByTags(className, {
189
+ filterText,
190
+ filterViewId,
191
+ filterDes,
192
+ });
193
+ Step.assert(this.step.stepId);
194
+ Step.assignIdsToNodes(nodes, this.step.stepId);
195
+ return nodes;
196
+ }
197
+
198
+ /**
199
+ * 查找所有匹配文本的节点
200
+ * @param text 要查找的文本
201
+ * @returns 节点数组
202
+ */
203
+ public async findByTextAllMatch(text: string): Promise<Node[]> {
204
+ Step.assert(this.step.stepId);
205
+ const nodes = await AssistsXAsync.findByTextAllMatch(text);
206
+ Step.assert(this.step.stepId);
207
+ Step.assignIdsToNodes(nodes, this.step.stepId);
208
+ return nodes;
209
+ }
210
+
211
+ /**
212
+ * 检查是否包含指定文本
213
+ * @param text 要检查的文本
214
+ * @returns 是否包含
215
+ */
216
+ public async containsText(text: string): Promise<boolean> {
217
+ Step.assert(this.step.stepId);
218
+ const result = await AssistsXAsync.containsText(text);
219
+ Step.assert(this.step.stepId);
220
+ return result;
221
+ }
222
+
223
+ /**
224
+ * 获取所有文本
225
+ * @returns 文本数组
226
+ */
227
+ public async getAllText(): Promise<string[]> {
228
+ Step.assert(this.step.stepId);
229
+ const texts = await AssistsXAsync.getAllText();
230
+ Step.assert(this.step.stepId);
231
+ return texts;
232
+ }
233
+
234
+ /**
235
+ * 执行点击手势
236
+ * @param x 横坐标
237
+ * @param y 纵坐标
238
+ * @param duration 持续时间(毫秒)
239
+ * @returns 是否成功
240
+ */
241
+ public async clickByGesture(
242
+ x: number,
243
+ y: number,
244
+ duration: number
245
+ ): Promise<boolean> {
246
+ Step.assert(this.step.stepId);
247
+ const result = await AssistsXAsync.clickByGesture(x, y, duration);
248
+ Step.assert(this.step.stepId);
249
+ return result;
250
+ }
251
+
252
+ public async longPressGestureAutoPaste(
253
+ point: { x: number; y: number },
254
+ text: string,
255
+ {
256
+ matchedPackageName,
257
+ matchedText,
258
+ timeoutMillis,
259
+ longPressDuration,
260
+ }: {
261
+ matchedPackageName?: string;
262
+ matchedText?: string;
263
+ timeoutMillis?: number;
264
+ longPressDuration?: number;
265
+ } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
266
+ ): Promise<boolean> {
267
+ Step.assert(this.step.stepId);
268
+ const result = await AssistsXAsync.longPressGestureAutoPaste(point, text, {
269
+ matchedPackageName,
270
+ matchedText,
271
+ timeoutMillis,
272
+ longPressDuration,
273
+ });
274
+ Step.assert(this.step.stepId);
275
+ return result;
276
+ }
277
+ public async getAppInfo(packageName: string): Promise<any> {
278
+ Step.assert(this.step.stepId);
279
+ const result = await AssistsXAsync.getAppInfo(packageName);
280
+ Step.assert(this.step.stepId);
281
+ return result;
282
+ }
283
+ public async performLinearGesture(
284
+ startPoint: { x: number; y: number },
285
+ endPoint: { x: number; y: number },
286
+ { duration }: { duration?: number } = {}
287
+ ): Promise<boolean> {
288
+ Step.assert(this.step.stepId);
289
+ const result = await AssistsXAsync.performLinearGesture(
290
+ startPoint,
291
+ endPoint,
292
+ {
293
+ duration,
294
+ }
295
+ );
296
+ Step.assert(this.step.stepId);
297
+ return result;
298
+ }
299
+
300
+ /**
301
+ * 返回操作
302
+ * @returns 是否成功
303
+ */
304
+ public async back(): Promise<boolean> {
305
+ Step.assert(this.step.stepId);
306
+ const result = await AssistsXAsync.back();
307
+ Step.assert(this.step.stepId);
308
+ return result;
309
+ }
310
+
311
+ /**
312
+ * 回到主页
313
+ * @returns 是否成功
314
+ */
315
+ public async home(): Promise<boolean> {
316
+ Step.assert(this.step.stepId);
317
+ const result = await AssistsXAsync.home();
318
+ Step.assert(this.step.stepId);
319
+ return result;
320
+ }
321
+
322
+ /**
323
+ * 打开通知栏
324
+ * @returns 是否成功
325
+ */
326
+ public async notifications(): Promise<boolean> {
327
+ Step.assert(this.step.stepId);
328
+ const result = await AssistsXAsync.notifications();
329
+ Step.assert(this.step.stepId);
330
+ return result;
331
+ }
332
+
333
+ /**
334
+ * 显示最近应用
335
+ * @returns 是否成功
336
+ */
337
+ public async recentApps(): Promise<boolean> {
338
+ Step.assert(this.step.stepId);
339
+ const result = await AssistsXAsync.recentApps();
340
+ Step.assert(this.step.stepId);
341
+ return result;
342
+ }
343
+
344
+ /**
345
+ * 获取屏幕尺寸
346
+ * @returns 屏幕尺寸对象
347
+ */
348
+ public async getScreenSize(): Promise<any> {
349
+ Step.assert(this.step.stepId);
350
+ const data = await AssistsXAsync.getScreenSize();
351
+ Step.assert(this.step.stepId);
352
+ return data;
353
+ }
354
+
355
+ /**
356
+ * 获取应用窗口尺寸
357
+ * @returns 应用窗口尺寸对象
358
+ */
359
+ public async getAppScreenSize(): Promise<any> {
360
+ Step.assert(this.step.stepId);
361
+ const data = await AssistsXAsync.getAppScreenSize();
362
+ Step.assert(this.step.stepId);
363
+ return data;
364
+ }
365
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * 步骤执行错误类
3
+ * 用于携带步骤执行过程中的错误信息和当前步骤对象
4
+ */
5
+ export class StepError extends Error {
6
+ /**
7
+ * 步骤实现函数名
8
+ */
9
+ readonly impl: string;
10
+
11
+ /**
12
+ * 步骤标签
13
+ */
14
+ readonly tag: string | undefined;
15
+
16
+ /**
17
+ * 步骤数据
18
+ */
19
+ readonly data: any | undefined;
20
+
21
+ /**
22
+ * 原始错误
23
+ */
24
+ readonly originalError: any;
25
+
26
+ /**
27
+ * 当前步骤对象
28
+ */
29
+ readonly currentStep: any | undefined;
30
+
31
+ constructor(
32
+ message: string,
33
+ impl: string,
34
+ tag: string | undefined,
35
+ data: any | undefined,
36
+ originalError: any,
37
+ currentStep: any | undefined
38
+ ) {
39
+ super(message);
40
+ this.name = "StepError";
41
+ this.impl = impl;
42
+ this.tag = tag;
43
+ this.data = data;
44
+ this.originalError = originalError;
45
+ this.currentStep = currentStep;
46
+ }
47
+ }
package/src/Utils.ts CHANGED
@@ -1,13 +1,22 @@
1
1
  // 导出工具函数
2
2
  export function sleep(ms: number): Promise<void> {
3
- return new Promise(resolve => setTimeout(resolve, ms));
3
+ return new Promise((resolve) => setTimeout(resolve, ms));
4
4
  }
5
5
 
6
6
  // 生成UUID
7
7
  export function generateUUID(): string {
8
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
9
- const r = Math.random() * 16 | 0;
10
- const v = c === 'x' ? r : (r & 0x3 | 0x8);
11
- return v.toString(16);
12
- });
13
- }
8
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
9
+ const r = (Math.random() * 16) | 0;
10
+ const v = c === "x" ? r : (r & 0x3) | 0x8;
11
+ return v.toString(16);
12
+ });
13
+ }
14
+
15
+ export function decodeBase64UTF8(base64: string): string {
16
+ const binary = atob(base64);
17
+ const bytes = new Uint8Array(binary.length);
18
+ for (let i = 0; i < binary.length; i++) {
19
+ bytes[i] = binary.charCodeAt(i);
20
+ }
21
+ return new TextDecoder("utf-8").decode(bytes);
22
+ }
package/src/global.d.ts CHANGED
@@ -1,15 +1,18 @@
1
- import { Node } from './Node';
1
+ import { Node } from "./Node";
2
2
 
3
3
  // 扩展 Window 接口
4
4
  declare global {
5
- interface Window {
6
- assistsx: {
7
- call(method: string): string | null;
8
- };
9
- assistsxCallback: (id: string, data: string) => void;
10
- onAccessibilityEvent: (event: any) => void;
11
- }
5
+ interface Window {
6
+ assistsx: {
7
+ call(method: string): string | null;
8
+ };
9
+ assistsxAsync: {
10
+ call(method: string): string | null;
11
+ };
12
+ assistsxCallback: (id: string, data: string) => void;
13
+ onAccessibilityEvent: (event: any) => void;
14
+ }
12
15
  }
13
16
 
14
17
  // 确保这个文件被视为模块
15
- export { };
18
+ export {};
package/src/index.ts CHANGED
@@ -1,10 +1,13 @@
1
- export * from './AssistsX';
2
- export * from './Node';
3
- export * from './CallMethod';
4
- export * from './CallResponse';
5
- export * from './NodeClassValue';
6
- export * from './Utils';
7
- export * from './Bounds';
8
- export * from './Step';
9
- export * from './StepStateStore';
10
- export * from './WindowFlags';
1
+ export * from "./AssistsX";
2
+ export * from "./Node";
3
+ export * from "./CallMethod";
4
+ export * from "./CallResponse";
5
+ export * from "./NodeClassValue";
6
+ export * from "./Utils";
7
+ export * from "./Bounds";
8
+ export * from "./Step";
9
+ export * from "./StepStateStore";
10
+ export * from "./WindowFlags";
11
+ export * from "./NodeAsync";
12
+ export * from "./AssistsXAsync";
13
+ export * from "./StepAsync";