@bettergi/utils 0.0.3 → 0.0.5

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/README.md CHANGED
@@ -10,7 +10,7 @@ npm install @bettergi/utils
10
10
 
11
11
  ### 图文识别
12
12
 
13
- > 对 RecognitionObject 代码的封装,对于简单的 OCR 操作,无需写复杂的代码。
13
+ > 对 RecognitionObject 代码的封装,对于简单的找图、找字操作,不再需要编写复杂的代码。
14
14
 
15
15
  ```ts
16
16
  import {
@@ -23,13 +23,13 @@ import {
23
23
  } from "@bettergi/utils";
24
24
 
25
25
  // 在整个画面内搜索图片,找不到返回 undefined
26
- const f1 = findImage("assets/关闭.png");
26
+ const i1 = findImage("assets/关闭.png");
27
27
 
28
28
  // 在指定方向上搜索图片,找不到返回 undefined
29
- const f2 = findImageInDirection("assets/关闭.png", "north-east");
29
+ const i2 = findImageInDirection("assets/关闭.png", "north-east");
30
30
 
31
31
  // 在指定区域内搜索图片,找不到返回 undefined
32
- const f3 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
32
+ const i3 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
33
33
 
34
34
  // 在整个画面内搜索文本(不包含、忽略大小写),找不到返回 undefined
35
35
  const t1 = findText("购买", false, true);
@@ -41,41 +41,78 @@ const t2 = findTextInDirection("师傅", true, true, "east");
41
41
  const t3 = findTextWithinBounds("确认", false, true, 960, 540, 960, 540);
42
42
  ```
43
43
 
44
- ### 行为
44
+ ### 行为流程
45
45
 
46
- > 对脚本工作流中常见行为的抽象,例如:等待 XXX 完成/出现/消失。
46
+ > 对脚本开发过程中常见工作流的抽象,例如:等待 XXX 完成/出现/消失。
47
47
 
48
48
  ```ts
49
49
  import { findImageInDirection, waitUntil } from "@bettergi/utils";
50
50
 
51
51
  // 等待直到找不到[关闭按钮] 或 5秒后超时,每隔1秒检查一次,期间按 Esc 键
52
52
  const done = await waitUntil(
53
- () => findImageInDirection("assets/关闭.png", "north-east") !== undefined,
53
+ () => findImageInDirection("assets/关闭.png", "north-east") === undefined,
54
54
  5000,
55
55
  1000,
56
56
  () => keyPress("ESCAPE")
57
57
  );
58
- if (!done) throw new Error("关闭页面");
58
+ if (!done) throw new Error("关闭页面超时");
59
59
  ```
60
60
 
61
- ### 存储
61
+ ### 鼠标操作
62
62
 
63
- > 对象数据持久化,通过代理实现自动存储。可以无感知地读取/更新数据,而无需考虑如何持久化。
63
+ > 对常见鼠标操作的封装,如鼠标滚动、拖拽等。
64
+
65
+ ```ts
66
+ import {
67
+ mouseScrollDown,
68
+ mouseScrollDownLines,
69
+ mouseScrollUp,
70
+ mouseScrollUpLines,
71
+ mouseSlide,
72
+ mouseSlideX,
73
+ mouseSlideY
74
+ } from "@bettergi/utils";
75
+
76
+ // 鼠标滚轮向上滚动 175 像素
77
+ await mouseScrollUp(175);
78
+
79
+ // 鼠标滚轮向下滚动 175 像素
80
+ await mouseScrollDown(175);
81
+
82
+ // 鼠标滚轮向上滚动 99 行,行高 175(默认:背包物品行高)
83
+ await mouseScrollUpLines(99);
84
+
85
+ // 鼠标滚轮向下滚动 1 行,行高 115(自定义:商店物品行高)
86
+ await mouseScrollDownLines(1, 115);
87
+
88
+ // 鼠标从 (745, 610) 拖拽到 (1280, 610)
89
+ await mouseSlide(745, 610, 1280, 610);
90
+
91
+ // 鼠标从 (745, 610) 向右拖拽 435 像素
92
+ await mouseSlideX(745, 610, 435);
93
+
94
+ // 鼠标从 (1290, 140) 向下拖拽 175 像素
95
+ await mouseSlideY(1290, 140, 175);
96
+ ```
97
+
98
+ ### 数据存储
99
+
100
+ > 对象数据持久化,通过 Proxy 实现自动存储。从而可以无感知地读取/更新数据,而无需考虑如何持久化。
64
101
 
65
102
  ```ts
66
103
  import { useStore } from "@bettergi/utils";
67
104
 
68
105
  // 创建/读取存储对象,保存到存储文件 store/state.json 中
69
- // 通过Proxy来实现:对存储对象的操作会同步保存到存储文件
70
106
  const state = useStore<{ lastUsedTime?: number; count: number }>("state");
71
107
  if (state?.lastUsedTime) {
72
108
  log.info(`欢迎回来!上次使用时间:${state.lastUsedTime},计数器已累计至:${state.count}`);
73
109
  }
74
110
  try {
111
+ // 模拟脚本运行期间状态的变化
75
112
  for (let i = 0; i < Math.floor(Math.random() * 100); i++) {
76
- state.count = (state.count || 0) + 1; // 同步保存到文件
113
+ state.count = (state.count || 0) + 1; // 自动同步保存到文件
77
114
  }
78
115
  } finally {
79
- state.lastUsedTime = Date.now(); // 同步保存到文件
116
+ state.lastUsedTime = Date.now(); // 自动同步保存到文件
80
117
  }
81
118
  ```
package/dist/mouse.d.ts CHANGED
@@ -22,3 +22,25 @@ export declare const mouseScrollUpLines: (lines: number, lineHeight?: number) =>
22
22
  * @param lineHeight 行高(默认值为175像素)
23
23
  */
24
24
  export declare const mouseScrollDownLines: (lines: number, lineHeight?: number) => Promise<void>;
25
+ /**
26
+ * 鼠标拖拽滑动到指定位置
27
+ * @param x1 起始水平方向偏移量(像素)
28
+ * @param y1 起始垂直方向偏移量(像素)
29
+ * @param x2 终止水平方向偏移量(像素)
30
+ * @param y2 终止垂直方向偏移量(像素)
31
+ */
32
+ export declare const mouseSlide: (x1: number, y1: number, x2: number, y2: number) => Promise<void>;
33
+ /**
34
+ * 鼠标水平拖拽滑动指定距离
35
+ * @param x 起始水平方向偏移量(像素)
36
+ * @param y 起始垂直方向偏移量(像素)
37
+ * @param distance 水平拖拽滑动距离(像素) 正数向右,负数向左
38
+ */
39
+ export declare const mouseSlideX: (x: number, y: number, distance: number) => Promise<void>;
40
+ /**
41
+ * 鼠标垂直拖拽滑动指定距离
42
+ * @param x 起始水平方向偏移量(像素)
43
+ * @param y 起始垂直方向偏移量(像素)
44
+ * @param distance 垂直拖拽滑动距离(像素) 正数向下,负数向上
45
+ */
46
+ export declare const mouseSlideY: (x: number, y: number, distance: number) => Promise<void>;
package/dist/mouse.js CHANGED
@@ -41,3 +41,37 @@ export const mouseScrollUpLines = (lines, lineHeight) => {
41
41
  export const mouseScrollDownLines = (lines, lineHeight) => {
42
42
  return mouseScrollDown(lines * (lineHeight ?? 175));
43
43
  };
44
+ /**
45
+ * 鼠标拖拽滑动到指定位置
46
+ * @param x1 起始水平方向偏移量(像素)
47
+ * @param y1 起始垂直方向偏移量(像素)
48
+ * @param x2 终止水平方向偏移量(像素)
49
+ * @param y2 终止垂直方向偏移量(像素)
50
+ */
51
+ export const mouseSlide = async (x1, y1, x2, y2) => {
52
+ moveMouseTo(x1, y1);
53
+ await sleep(50);
54
+ leftButtonDown();
55
+ await sleep(50);
56
+ moveMouseTo(x2, y2);
57
+ await sleep(50);
58
+ leftButtonUp();
59
+ };
60
+ /**
61
+ * 鼠标水平拖拽滑动指定距离
62
+ * @param x 起始水平方向偏移量(像素)
63
+ * @param y 起始垂直方向偏移量(像素)
64
+ * @param distance 水平拖拽滑动距离(像素) 正数向右,负数向左
65
+ */
66
+ export const mouseSlideX = (x, y, distance) => {
67
+ return mouseSlide(x, y, x + distance, y);
68
+ };
69
+ /**
70
+ * 鼠标垂直拖拽滑动指定距离
71
+ * @param x 起始水平方向偏移量(像素)
72
+ * @param y 起始垂直方向偏移量(像素)
73
+ * @param distance 垂直拖拽滑动距离(像素) 正数向下,负数向上
74
+ */
75
+ export const mouseSlideY = (x, y, distance) => {
76
+ return mouseSlide(x, y, x, y + distance);
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Utils for BetterGI JavaScript Development",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",