@bettergi/utils 0.0.1
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 +21 -0
- package/dist/action.d.ts +10 -0
- package/dist/action.js +19 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/mouse.d.ts +24 -0
- package/dist/mouse.js +43 -0
- package/dist/ocr.d.ts +38 -0
- package/dist/ocr.js +82 -0
- package/dist/polyfill.d.ts +1 -0
- package/dist/polyfill.js +10 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Utils for [Better Genshin Impact](https://github.com/babalae/better-genshin-impact) JavaScript development.
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```shell
|
|
6
|
+
npm install @bettergi/utils
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Example
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { findImage } from "@bettergi/utils";
|
|
13
|
+
|
|
14
|
+
const mailIcon = findImage("./assets/mail.png");
|
|
15
|
+
|
|
16
|
+
mailIcon?.click();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Related Tools
|
|
20
|
+
|
|
21
|
+
[@bettergi/create-script](https://www.npmjs.com/package/@bettergi/create-script)
|
package/dist/action.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 等待直到条件满足或超时
|
|
3
|
+
* @param condition 等待的条件判断函数,返回 true 表示条件满足
|
|
4
|
+
* @param timeout 超时时间(毫秒),默认 5000 毫秒
|
|
5
|
+
* @param interval 等待间隔(毫秒),默认 200 毫秒
|
|
6
|
+
* @param action 每次等待循环中执行的操作(可选)
|
|
7
|
+
* @returns - true 在超时前条件已满足
|
|
8
|
+
* - false 在超时后条件仍未满足
|
|
9
|
+
*/
|
|
10
|
+
export declare const waitUntil: (condition: () => boolean, timeout?: number, interval?: number, action?: () => void) => Promise<boolean>;
|
package/dist/action.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 等待直到条件满足或超时
|
|
3
|
+
* @param condition 等待的条件判断函数,返回 true 表示条件满足
|
|
4
|
+
* @param timeout 超时时间(毫秒),默认 5000 毫秒
|
|
5
|
+
* @param interval 等待间隔(毫秒),默认 200 毫秒
|
|
6
|
+
* @param action 每次等待循环中执行的操作(可选)
|
|
7
|
+
* @returns - true 在超时前条件已满足
|
|
8
|
+
* - false 在超时后条件仍未满足
|
|
9
|
+
*/
|
|
10
|
+
export const waitUntil = async (condition, timeout, interval, action) => {
|
|
11
|
+
const deadline = Date.now() + (timeout ?? 5 * 1000);
|
|
12
|
+
while (Date.now() < deadline) {
|
|
13
|
+
if (condition())
|
|
14
|
+
return true;
|
|
15
|
+
action?.();
|
|
16
|
+
await sleep(interval ?? 200);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/mouse.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 鼠标滚轮向上滚动指定高度
|
|
3
|
+
* @param height 滚动高度
|
|
4
|
+
* @param algorithm 自定义滚动算法函数,接收高度参数并返回滚动次数(默认算法为每18像素滚动一次)
|
|
5
|
+
*/
|
|
6
|
+
export declare const mouseScrollUp: (height: number, algorithm?: (h: number) => number) => Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* 鼠标滚轮向下滚动指定高度
|
|
9
|
+
* @param height 滚动高度
|
|
10
|
+
* @param algorithm 自定义滚动算法函数,接收高度参数并返回滚动次数(默认算法为每18像素滚动一次)
|
|
11
|
+
*/
|
|
12
|
+
export declare const mouseScrollDown: (height: number, algorithm?: (height: number) => number) => Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* 鼠标滚轮向上滚动指定行数
|
|
15
|
+
* @param lines 滚动行数
|
|
16
|
+
* @param lineHeight 行高(默认值为175像素)
|
|
17
|
+
*/
|
|
18
|
+
export declare const mouseScrollUpLines: (lines: number, lineHeight?: number) => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* 鼠标滚轮向下滚动指定行数
|
|
21
|
+
* @param lines 滚动行数
|
|
22
|
+
* @param lineHeight 行高(默认值为175像素)
|
|
23
|
+
*/
|
|
24
|
+
export declare const mouseScrollDownLines: (lines: number, lineHeight?: number) => Promise<void>;
|
package/dist/mouse.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const _simulateScroll = async (scrollAmountInClicks, times) => {
|
|
2
|
+
const script = {
|
|
3
|
+
macroEvents: Array(times).fill({ type: 6, mouseX: 0, mouseY: scrollAmountInClicks, time: 0 }),
|
|
4
|
+
info: { name: "", description: "", x: 0, y: 0, width: 1920, height: 1080, recordDpi: 1.25 }
|
|
5
|
+
};
|
|
6
|
+
await keyMouseScript.run(JSON.stringify(script));
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* 鼠标滚轮向上滚动指定高度
|
|
10
|
+
* @param height 滚动高度
|
|
11
|
+
* @param algorithm 自定义滚动算法函数,接收高度参数并返回滚动次数(默认算法为每18像素滚动一次)
|
|
12
|
+
*/
|
|
13
|
+
export const mouseScrollUp = (height, algorithm) => {
|
|
14
|
+
const scrollAlgorithm = algorithm ?? (h => Math.floor(h / 18));
|
|
15
|
+
const scrollTimes = scrollAlgorithm(height);
|
|
16
|
+
return _simulateScroll(120, scrollTimes);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* 鼠标滚轮向下滚动指定高度
|
|
20
|
+
* @param height 滚动高度
|
|
21
|
+
* @param algorithm 自定义滚动算法函数,接收高度参数并返回滚动次数(默认算法为每18像素滚动一次)
|
|
22
|
+
*/
|
|
23
|
+
export const mouseScrollDown = (height, algorithm) => {
|
|
24
|
+
const scrollAlgorithm = algorithm ?? (h => Math.floor(h / 18));
|
|
25
|
+
const scrollTimes = scrollAlgorithm(height);
|
|
26
|
+
return _simulateScroll(-120, scrollTimes);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* 鼠标滚轮向上滚动指定行数
|
|
30
|
+
* @param lines 滚动行数
|
|
31
|
+
* @param lineHeight 行高(默认值为175像素)
|
|
32
|
+
*/
|
|
33
|
+
export const mouseScrollUpLines = (lines, lineHeight) => {
|
|
34
|
+
return mouseScrollUp(lines * (lineHeight ?? 175));
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* 鼠标滚轮向下滚动指定行数
|
|
38
|
+
* @param lines 滚动行数
|
|
39
|
+
* @param lineHeight 行高(默认值为175像素)
|
|
40
|
+
*/
|
|
41
|
+
export const mouseScrollDownLines = (lines, lineHeight) => {
|
|
42
|
+
return mouseScrollDown(lines * (lineHeight ?? 175));
|
|
43
|
+
};
|
package/dist/ocr.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Region } from "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
|
|
2
|
+
/**
|
|
3
|
+
* 搜索图片
|
|
4
|
+
* @param path 图片路径
|
|
5
|
+
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
6
|
+
*/
|
|
7
|
+
export declare const findImage: (path: string) => Region | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* 在指定区域内搜索图片
|
|
10
|
+
* @param path 图片路径
|
|
11
|
+
* @param x - 水平移动偏移量(像素)
|
|
12
|
+
* @param y - 垂直移动偏移量(像素)
|
|
13
|
+
* @param w 宽度
|
|
14
|
+
* @param h 高度
|
|
15
|
+
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
16
|
+
*/
|
|
17
|
+
export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number) => Region | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* 搜索文本
|
|
20
|
+
* @param text 待搜索文本
|
|
21
|
+
* @param contains 是否包含
|
|
22
|
+
* @param ignoreCase 是否忽略大小写
|
|
23
|
+
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
24
|
+
*/
|
|
25
|
+
export declare const findText: (text: string, contains: boolean, ignoreCase: boolean) => Region | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* 在指定区域内搜索文本
|
|
28
|
+
* @param text 待搜索文本
|
|
29
|
+
* @param contains 是否包含
|
|
30
|
+
* @param ignoreCase 是否忽略大小写
|
|
31
|
+
* @param x 水平移动偏移量(像素)
|
|
32
|
+
* @param x - 水平移动偏移量(像素)
|
|
33
|
+
* @param y - 垂直移动偏移量(像素)
|
|
34
|
+
* @param w 宽度
|
|
35
|
+
* @param h 高度
|
|
36
|
+
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
37
|
+
*/
|
|
38
|
+
export declare const findTextWithinBounds: (text: string, contains: boolean, ignoreCase: boolean, x: number, y: number, w: number, h: number) => Region | undefined;
|
package/dist/ocr.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const findFirst = (im, ro, predicate) => {
|
|
2
|
+
const candidates = im.findMulti(ro);
|
|
3
|
+
for (let i = 0; i < candidates.count; i++) {
|
|
4
|
+
if (predicate(candidates[i])) {
|
|
5
|
+
return candidates[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return undefined;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 搜索图片
|
|
12
|
+
* @param path 图片路径
|
|
13
|
+
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
14
|
+
*/
|
|
15
|
+
export const findImage = (path) => {
|
|
16
|
+
try {
|
|
17
|
+
const im = captureGameRegion();
|
|
18
|
+
const ro = RecognitionObject.templateMatch(file.readImageMatSync(path));
|
|
19
|
+
return findFirst(im, ro, region => region.isExist());
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
err?.message && log.warn(`${err.message}`);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* 在指定区域内搜索图片
|
|
27
|
+
* @param path 图片路径
|
|
28
|
+
* @param x - 水平移动偏移量(像素)
|
|
29
|
+
* @param y - 垂直移动偏移量(像素)
|
|
30
|
+
* @param w 宽度
|
|
31
|
+
* @param h 高度
|
|
32
|
+
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
33
|
+
*/
|
|
34
|
+
export const findImageWithinBounds = (path, x, y, w, h) => {
|
|
35
|
+
try {
|
|
36
|
+
const im = captureGameRegion();
|
|
37
|
+
const ro = RecognitionObject.templateMatch(file.readImageMatSync(path), x, y, w, h);
|
|
38
|
+
return findFirst(im, ro, region => region.isExist());
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
err?.message && log.warn(`${err.message}`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* 搜索文本
|
|
46
|
+
* @param text 待搜索文本
|
|
47
|
+
* @param contains 是否包含
|
|
48
|
+
* @param ignoreCase 是否忽略大小写
|
|
49
|
+
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
50
|
+
*/
|
|
51
|
+
export const findText = (text, contains, ignoreCase) => {
|
|
52
|
+
const searchText = ignoreCase ? text.toLowerCase() : text;
|
|
53
|
+
const im = captureGameRegion();
|
|
54
|
+
const ro = RecognitionObject.ocrThis;
|
|
55
|
+
return findFirst(im, ro, region => {
|
|
56
|
+
const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
|
|
57
|
+
const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
|
|
58
|
+
return isMatch && region.isExist();
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* 在指定区域内搜索文本
|
|
63
|
+
* @param text 待搜索文本
|
|
64
|
+
* @param contains 是否包含
|
|
65
|
+
* @param ignoreCase 是否忽略大小写
|
|
66
|
+
* @param x 水平移动偏移量(像素)
|
|
67
|
+
* @param x - 水平移动偏移量(像素)
|
|
68
|
+
* @param y - 垂直移动偏移量(像素)
|
|
69
|
+
* @param w 宽度
|
|
70
|
+
* @param h 高度
|
|
71
|
+
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
72
|
+
*/
|
|
73
|
+
export const findTextWithinBounds = (text, contains, ignoreCase, x, y, w, h) => {
|
|
74
|
+
const searchText = ignoreCase ? text.toLowerCase() : text;
|
|
75
|
+
const im = captureGameRegion();
|
|
76
|
+
const ro = RecognitionObject.ocr(x, y, w, h);
|
|
77
|
+
return findFirst(im, ro, region => {
|
|
78
|
+
const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
|
|
79
|
+
const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
|
|
80
|
+
return isMatch && region.isExist();
|
|
81
|
+
});
|
|
82
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const verticalScroll: (scrollAmountInClicks: number) => void;
|
package/dist/polyfill.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { _simulateScroll } from "./mouse";
|
|
2
|
+
export const verticalScroll = (scrollAmountInClicks) => {
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
if (globalThis.verticalScroll) {
|
|
5
|
+
globalThis.verticalScroll(scrollAmountInClicks);
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
_simulateScroll(scrollAmountInClicks, 1);
|
|
9
|
+
}
|
|
10
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bettergi/utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Utils for BetterGI JavaScript Development",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "Bread Grocery<https://github.com/breadgrocery>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/breadgrocery/bettergi-script-toolchain.git",
|
|
11
|
+
"directory": "packages/utils"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/breadgrocery/bettergi-script-toolchain/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/breadgrocery/bettergi-script-toolchain/tree/main/packages/utils",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"keywords": [
|
|
28
|
+
"bettergi",
|
|
29
|
+
"utils"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "tsc --watch",
|
|
33
|
+
"build": "tsc"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@bettergi/types": "^0.0.5",
|
|
37
|
+
"typescript": "^5.6.3"
|
|
38
|
+
}
|
|
39
|
+
}
|