@bettergi/utils 0.1.7 → 0.1.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/README.md +37 -13
- package/dist/exception.d.ts +16 -0
- package/dist/exception.js +24 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ pnpm install @bettergi/utils
|
|
|
16
16
|
|
|
17
17
|
## 函数清单
|
|
18
18
|
|
|
19
|
-
###
|
|
19
|
+
### 游戏操作
|
|
20
20
|
|
|
21
21
|
> 常见游戏内操作封装,省去手动实现的繁琐。
|
|
22
22
|
|
|
@@ -36,7 +36,7 @@ await setTimeTo(12, 35);
|
|
|
36
36
|
// 调整游戏时间段
|
|
37
37
|
await setTime("evening");
|
|
38
38
|
|
|
39
|
-
// tab
|
|
39
|
+
// tab 翻页到指定页面(假设当前已打开背包)
|
|
40
40
|
await navigateToTab(() => {
|
|
41
41
|
return findTextWithinBounds("小道具", 0, 0, 220, 96) !== undefined;
|
|
42
42
|
});
|
|
@@ -47,6 +47,7 @@ await navigateToTab(() => {
|
|
|
47
47
|
> 对 RecognitionObject 代码的封装,对于简单的找图、找字操作,不再需要编写复杂的代码。
|
|
48
48
|
|
|
49
49
|
```ts
|
|
50
|
+
// 在整个画面内搜索图片,找不到返回 undefined
|
|
50
51
|
const i1 = findImage("assets/关闭.png");
|
|
51
52
|
|
|
52
53
|
// 在指定方向上搜索图片,找不到返回 undefined
|
|
@@ -70,7 +71,7 @@ const t3 = findTextWithinBounds("确认", 960, 540, 960, 540);
|
|
|
70
71
|
> 对脚本开发过程中常见工作流的抽象,例如: 等待/断言 操作/元素/区域 完成/出现/消失。
|
|
71
72
|
|
|
72
73
|
```ts
|
|
73
|
-
// 等待直到找不到[关闭按钮]
|
|
74
|
+
// 等待直到找不到 [关闭按钮] ,重试5次,每隔1秒重试一次,期间按 Esc 键
|
|
74
75
|
const done = await waitForAction(
|
|
75
76
|
() => findImageInDirection("assets/关闭.png", "north-east") === undefined,
|
|
76
77
|
() => keyPress("ESCAPE"),
|
|
@@ -78,15 +79,15 @@ const done = await waitForAction(
|
|
|
78
79
|
);
|
|
79
80
|
if (!done) throw new Error("关闭页面超时");
|
|
80
81
|
|
|
81
|
-
// 断言 "
|
|
82
|
+
// 断言 "生日" 文字区域即将出现,重试5次,每隔1秒重试一次,期间按 Esc 键
|
|
82
83
|
await assertRegionAppearing(
|
|
83
|
-
() => findTextInDirection("
|
|
84
|
+
() => findTextInDirection("生日", "north-west"),
|
|
84
85
|
"打开派蒙菜单超时",
|
|
85
86
|
() => keyPress("ESCAPE"),
|
|
86
87
|
{ maxAttempts: 5, retryInterval: 1000 }
|
|
87
88
|
);
|
|
88
89
|
|
|
89
|
-
// 断言 "购买"
|
|
90
|
+
// 断言 "购买" 区域不存在,否则抛出异常,重试5次,每隔1秒重试一次,期间如果存在 "购买" 按钮则点击
|
|
90
91
|
const findButton = () => findTextWithinBounds("购买", 500, 740, 900, 110);
|
|
91
92
|
await assertRegionDisappearing(findButton, "点击购买按钮超时", () => findButton()?.click(), {
|
|
92
93
|
maxAttempts: 5,
|
|
@@ -123,10 +124,11 @@ await mouseScrollDownLines(1, 115);
|
|
|
123
124
|
> 对象数据持久化,通过 Proxy 实现自动存储。从而可以无感知地读取/更新数据,而无需考虑如何持久化。
|
|
124
125
|
|
|
125
126
|
```ts
|
|
126
|
-
|
|
127
|
+
// 创建/读取存储对象,保存到存储文件 store/my-data.json 中
|
|
128
|
+
const state = useStore<{ lastUsedTime?: number; count: number }>("my-data");
|
|
129
|
+
// 默认值版本
|
|
130
|
+
// const state = useStoreWithDefaults("my-data", { lastUsedTime: 0, count: 0 });
|
|
127
131
|
|
|
128
|
-
// 创建/读取存储对象,保存到存储文件 store/state.json 中
|
|
129
|
-
const state = useStore<{ lastUsedTime?: number; count: number }>("state");
|
|
130
132
|
if (state?.lastUsedTime) {
|
|
131
133
|
log.info(`欢迎回来!上次使用时间: ${state.lastUsedTime},计数器已累计至: ${state.count}`);
|
|
132
134
|
}
|
|
@@ -143,15 +145,16 @@ try {
|
|
|
143
145
|
### 网络请求
|
|
144
146
|
|
|
145
147
|
> 对网络请求的简易封装。
|
|
148
|
+
>
|
|
149
|
+
> 提示:需要在 `manifest.json` 文件中配置 `http_allowed_urls`,并在 `调度器` -> `修改通用配置` 中启用。
|
|
146
150
|
|
|
147
151
|
```ts
|
|
148
|
-
import { getForBody, postForBody } from "@bettergi/utils";
|
|
149
|
-
|
|
150
152
|
// 发送 GET 请求获取响应体内容
|
|
151
|
-
// 提示: 需要在 `manifest.json` 文件中配置 `http_allowed_urls`,并在 调度器 -> 修改通用配置 中启用
|
|
152
153
|
const body1 = await getForBody("https://example.com/", undefined, { "User-Agent": "BetterGI" });
|
|
153
|
-
const body2 = await postForBody("https://example.com/", undefined, { "User-Agent": "BetterGI" });
|
|
154
154
|
log.info(`GET 请求响应体内容${body1}`);
|
|
155
|
+
|
|
156
|
+
// 发送 POST 请求获取响应体内容
|
|
157
|
+
const body2 = await postForBody("https://example.com/", undefined, { "User-Agent": "BetterGI" });
|
|
155
158
|
log.info(`POST 请求响应体内容${body2}`);
|
|
156
159
|
```
|
|
157
160
|
|
|
@@ -170,10 +173,31 @@ const lines = readLinesSync("assets/data.txt", { notBlank: true, trim: true, dis
|
|
|
170
173
|
```ts
|
|
171
174
|
// 获取下一个(含当日)凌晨4点的时间
|
|
172
175
|
const d1 = getNextDay4AM();
|
|
176
|
+
|
|
173
177
|
// 获取下一个(含当日)周一凌晨4点的时间
|
|
174
178
|
const d2 = getNextMonday4AM();
|
|
175
179
|
```
|
|
176
180
|
|
|
181
|
+
### 异常
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
// 获取异常信息字符串
|
|
185
|
+
const message = getErrorMessage(err);
|
|
186
|
+
|
|
187
|
+
// 判断是否为任务取消异常
|
|
188
|
+
const isTaskCanceled = isTaskCanceledException(err);
|
|
189
|
+
|
|
190
|
+
// 重复执行某个可能失败的异步操作,但是发生主机异常(如任务取消)时停止
|
|
191
|
+
for (let i = 0; i < 1000; i++) {
|
|
192
|
+
try {
|
|
193
|
+
await sleep(i);
|
|
194
|
+
} catch (err: any) {
|
|
195
|
+
if (isHostException(err)) throw err;
|
|
196
|
+
log.info(`第 ${i} 次运行失败,错误信息:${err.message}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
177
201
|
### 杂项
|
|
178
202
|
|
|
179
203
|
```ts
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取错误信息字符串
|
|
3
|
+
* @param err 异常对象
|
|
4
|
+
* @returns 错误信息字符串
|
|
5
|
+
*/
|
|
6
|
+
export declare const getErrorMessage: (err: any) => string;
|
|
7
|
+
/**
|
|
8
|
+
* 判断是否为主机异常
|
|
9
|
+
* @param err 异常对象
|
|
10
|
+
*/
|
|
11
|
+
export declare const isHostException: (err: any) => any;
|
|
12
|
+
/**
|
|
13
|
+
* 判断是否为任务取消异常
|
|
14
|
+
* @param err 异常对象
|
|
15
|
+
*/
|
|
16
|
+
export declare const isTaskCanceledException: (err: any) => any;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取错误信息字符串
|
|
3
|
+
* @param err 异常对象
|
|
4
|
+
* @returns 错误信息字符串
|
|
5
|
+
*/
|
|
6
|
+
export const getErrorMessage = (err) => {
|
|
7
|
+
if (err && "message" in err && typeof err.message === "string")
|
|
8
|
+
return err.message;
|
|
9
|
+
return err && typeof err === "object" ? JSON.stringify(err) : "Unknown error";
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* 判断是否为主机异常
|
|
13
|
+
* @param err 异常对象
|
|
14
|
+
*/
|
|
15
|
+
export const isHostException = (err) => {
|
|
16
|
+
return err && "hostException" in err;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* 判断是否为任务取消异常
|
|
20
|
+
* @param err 异常对象
|
|
21
|
+
*/
|
|
22
|
+
export const isTaskCanceledException = (err) => {
|
|
23
|
+
return isHostException(err) && getErrorMessage(err).includes("task was canceled");
|
|
24
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED