vite-plugin-autojs6 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +411 -2
- package/bin/create.mjs +230 -0
- package/dist/plugin/autojs6.d.ts +17 -0
- package/dist/plugin/builtin.d.ts +21 -0
- package/dist/plugin/deploy.d.ts +44 -0
- package/dist/plugin/index.cjs +341 -0
- package/dist/plugin/index.d.ts +13 -0
- package/dist/plugin/index.js +341 -0
- package/dist/plugin/options.d.ts +81 -0
- package/package.json +64 -3
- package/template/.oxfmtrc.jsonc +39 -0
- package/template/.oxlintrc.jsonc +76 -0
- package/template/README.md +109 -0
- package/template/package.json +27 -0
- package/template/project.json +15 -0
- package/template/res/icon.png +0 -0
- package/template/res/layout/main.xml +26 -0
- package/template/src/main.ts +30 -0
- package/template/src/main.ui.ts +29 -0
- package/template/tsconfig.json +24 -0
- package/template/types/globals.d.ts +19 -0
- package/template/vite.config.ts +36 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# AutoJs6 Script
|
|
2
|
+
|
|
3
|
+
用 TypeScript 写 [AutoJs6](https://github.com/SuperMonster003/AutoJs6) 脚本, 由 Vite 打包成 AutoJs6 可以直接执行的 `main.js`.
|
|
4
|
+
|
|
5
|
+
- 构建由 [`vite-plugin-autojs6`](https://www.npmjs.com/package/vite-plugin-autojs6) 的 Vite 插件完成: 单文件 CJS 产物、内置模块拦截、`"ui";` 注入、资源同步
|
|
6
|
+
- 脚本里直接用 AutoJs6 的全局 API (`device` / `app` / `ocr` / `images` ...), 不用 import 任何 SDK
|
|
7
|
+
- 代码提示来自官方声明文件 [`@sm003/autojs6-dts`](https://github.com/SuperMonster003/AutoJs6-TypeScript-Declarations)
|
|
8
|
+
|
|
9
|
+
## 快速开始
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install
|
|
13
|
+
npm run dev # 监听构建, 产物在 dist/
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
把 **`dist` 目录**部署到手机上 (VSCode + AutoJs6 扩展 / adb push / 文件管理器), 即可运行.
|
|
17
|
+
|
|
18
|
+
想省掉手动那一步? 在 `vite.config.ts` 里打开 `deploy`, 每次构建结束由 VSCode 的 AutoJs6 扩展自动推送:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
autojs6({
|
|
22
|
+
// ...
|
|
23
|
+
deploy: { enabled: true, command: "save" } // 'run' 则保存后立即运行
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 脚本
|
|
28
|
+
|
|
29
|
+
| 命令 | 作用 |
|
|
30
|
+
| ---------------------- | -------------------------------------------------- |
|
|
31
|
+
| `npm run dev` | 开发构建, 监听变更, 不压缩 |
|
|
32
|
+
| `npm run dev:ui` | 同上, 但使用 `src/main.ui.ts` 且产物首行是 `"ui";` |
|
|
33
|
+
| `npm run build` | 正式构建 (压缩) |
|
|
34
|
+
| `npm run build:ui` | 正式构建 + UI 模式 |
|
|
35
|
+
| `npm run typecheck` | 只做类型检查 |
|
|
36
|
+
| `npm run lint` | oxlint 校验 (ES2018 限制) |
|
|
37
|
+
| `npm run lint:fix` | oxlint 自动修复 |
|
|
38
|
+
| `npm run format` | oxfmt 格式化 |
|
|
39
|
+
| `npm run format:check` | 只检查格式, 不写入 |
|
|
40
|
+
| `npm run check` | typecheck + lint + 格式检查 + 构建 |
|
|
41
|
+
|
|
42
|
+
## 写脚本
|
|
43
|
+
|
|
44
|
+
全局对象直接可用, 全程有类型提示 (Ctrl/⌘ + 点击可以跳到声明文件):
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
log(`${device.brand} ${device.model}`);
|
|
48
|
+
toast("已启动");
|
|
49
|
+
|
|
50
|
+
const lines = ocr.recognizeText(); // string[]
|
|
51
|
+
const point = images.findColor(images.captureScreen(), "#FF0000");
|
|
52
|
+
click(540, 1200);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
需要 Node 兼容模块时才 import, 且只支持默认导入:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import path from "path"; // → require("path")
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 目录结构
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
.
|
|
65
|
+
├── src/
|
|
66
|
+
│ ├── main.ts # 普通脚本入口
|
|
67
|
+
│ └── main.ui.ts # UI 模式入口
|
|
68
|
+
├── res/
|
|
69
|
+
│ ├── icon.png
|
|
70
|
+
│ └── layout/main.xml # UI 布局
|
|
71
|
+
├── types/globals.d.ts # 编译期常量 / ?raw 声明
|
|
72
|
+
├── .oxfmtrc.jsonc # oxfmt 格式化配置
|
|
73
|
+
├── .oxlintrc.jsonc # oxlint 校验配置 (限制 ES2018)
|
|
74
|
+
├── project.json # AutoJs6 项目描述 (会同步到 dist)
|
|
75
|
+
├── vite.config.ts
|
|
76
|
+
└── tsconfig.json
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`tsconfig.json` 里的 `types: ["@sm003/autojs6-dts"]` 是类型提示的来源, 不要删.
|
|
80
|
+
|
|
81
|
+
## 代码风格与 ES2018 限制
|
|
82
|
+
|
|
83
|
+
格式化用 [oxfmt](https://oxc.rs/docs/guide/usage/formatter), 校验用 [oxlint](https://oxc.rs/docs/guide/usage/linter):
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
npm run format # 双引号 / 分号 / 无尾随逗号 / 2 空格 / 100 列
|
|
87
|
+
npm run lint # 检查
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`.oxlintrc.jsonc` 把可用语法/API 限制在 **ES2018** (AutoJs6 的 Rhino 引擎只到这一档, 且构建不做 polyfill):
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
arr.flat(); // ✖ ES2019
|
|
94
|
+
arr.flatMap((x) => x); // ✖ ES2019
|
|
95
|
+
text.replaceAll("-", "+"); // ✖ ES2021, 改用 replace(/-/g, '+')
|
|
96
|
+
text.trimStart(); // ✖ ES2019, 改用 replace(/^\s+/, '')
|
|
97
|
+
Object.fromEntries(entries); // ✖ ES2019
|
|
98
|
+
promise.allSettled(list); // ✖ ES2020
|
|
99
|
+
globalThis.foo; // ✖ ES2020, 直接用具体全局对象
|
|
100
|
+
arr.at(-1); // ✖ ES2022, 改用 arr[arr.length - 1]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`no-undef` 是关闭的: `device` / `app` / `ocr` 这些 AutoJs6 全局由声明文件提供类型, 未定义变量交给 `tsc` 检查.
|
|
104
|
+
|
|
105
|
+
## 注意
|
|
106
|
+
|
|
107
|
+
- XML 布局不能写成字面量 (E4X), 会直接编译不过; 布局放 `res/layout/*.xml` 用 `ui.layoutFile()` 加载.
|
|
108
|
+
- 动态 `import()` 会被内联, 不能用来做懒加载.
|
|
109
|
+
- 顶层 `await` 不支持, 异步逻辑写在 `async` 函数里.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "autojs6-script",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "AutoJs6 脚本项目 (vite-plugin-autojs6 + Vite + TypeScript)",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "vite build --watch --mode development",
|
|
9
|
+
"dev:ui": "vite build --watch --mode development-ui",
|
|
10
|
+
"build": "vite build --mode production",
|
|
11
|
+
"build:ui": "vite build --mode production-ui",
|
|
12
|
+
"typecheck": "tsc --noEmit",
|
|
13
|
+
"lint": "oxlint",
|
|
14
|
+
"lint:fix": "oxlint --fix",
|
|
15
|
+
"format": "oxfmt",
|
|
16
|
+
"format:check": "oxfmt --check",
|
|
17
|
+
"check": "npm run typecheck && npm run lint && npm run format:check && npm run build"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@sm003/autojs6-dts": "^4.87.0",
|
|
21
|
+
"vite-plugin-autojs6": "file:..",
|
|
22
|
+
"oxfmt": "^0.77.0",
|
|
23
|
+
"oxlint": "^1.92.0",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vite": "^7.3.8"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "AutoJs6 Script",
|
|
3
|
+
"versionName": "1.0.0",
|
|
4
|
+
"versionCode": 1,
|
|
5
|
+
"packageName": "com.example.autojs6.script",
|
|
6
|
+
"main": "main.js",
|
|
7
|
+
"icon": "res/icon.png",
|
|
8
|
+
"useFeatures": [],
|
|
9
|
+
"launchConfig": {
|
|
10
|
+
"displaySplash": true,
|
|
11
|
+
"hideLogs": false,
|
|
12
|
+
"stableMode": false
|
|
13
|
+
},
|
|
14
|
+
"ignore": ["node_modules", "src", "types", ".git", ".vscode", "dist"]
|
|
15
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!-- AutoJs6 UI 模式示例布局 (相对路径基于脚本工作目录) -->
|
|
3
|
+
<vertical bg="#FFFFFF" padding="24">
|
|
4
|
+
|
|
5
|
+
<text
|
|
6
|
+
id="title"
|
|
7
|
+
text="AutoJs6 + Vite"
|
|
8
|
+
textColor="#111827"
|
|
9
|
+
textSize="22sp"
|
|
10
|
+
textStyle="bold" />
|
|
11
|
+
|
|
12
|
+
<text
|
|
13
|
+
id="meta"
|
|
14
|
+
text="TypeScript · Vite · Rhino"
|
|
15
|
+
marginTop="6"
|
|
16
|
+
textColor="#6B7280"
|
|
17
|
+
textSize="13sp" />
|
|
18
|
+
|
|
19
|
+
<button
|
|
20
|
+
id="btnClose"
|
|
21
|
+
marginTop="28"
|
|
22
|
+
h="48dp"
|
|
23
|
+
text="关闭"
|
|
24
|
+
w="*" />
|
|
25
|
+
|
|
26
|
+
</vertical>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 入口脚本.
|
|
3
|
+
*
|
|
4
|
+
* `device` / `app` / `ocr` / `images` / `toast` ... 都是 AutoJs6 的全局对象,
|
|
5
|
+
* 直接用即可, 不需要 import; 类型提示来自 @sm003/autojs6-dts.
|
|
6
|
+
*/
|
|
7
|
+
async function main(): Promise<void> {
|
|
8
|
+
log(`${device.brand} ${device.model}`);
|
|
9
|
+
log(`Android ${device.release} (API ${device.sdkInt})`);
|
|
10
|
+
log(`屏幕 ${device.width} x ${device.height} @ ${device.density}dpi`);
|
|
11
|
+
toast(`${device.model} · AutoJs6 已就绪`);
|
|
12
|
+
// 文字识别: 需要「屏幕截图」权限 + 已安装 OCR 插件
|
|
13
|
+
try {
|
|
14
|
+
const lines = ocr.recognizeText(); // string[]
|
|
15
|
+
log(`识别到 ${lines.length} 行`);
|
|
16
|
+
log(lines.slice(0, 5).join("\n"));
|
|
17
|
+
} catch (error) {
|
|
18
|
+
log("OCR 不可用: 请在 AutoJs6 中授予截图权限, 并安装 OCR 插件");
|
|
19
|
+
log(error);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
await new Promise<void>((resolve) => {
|
|
23
|
+
setTimeout(resolve, 1000);
|
|
24
|
+
});
|
|
25
|
+
log("完成");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
main().catch((error: unknown) => {
|
|
29
|
+
console.error(error);
|
|
30
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI 模式入口 (npm run dev:ui / npm run build:ui).
|
|
3
|
+
*
|
|
4
|
+
* 注意:
|
|
5
|
+
* - 产物首行的 `"ui";` 指令由 vite-plugin-autojs6 插件写入, 源码里不要手写;
|
|
6
|
+
* - `ui` 是 AutoJs6 的全局对象, 直接使用即可.
|
|
7
|
+
*/
|
|
8
|
+
ui.layoutFile("res/layout/main.xml");
|
|
9
|
+
|
|
10
|
+
// 声明文件里 ui.findView() 返回的是 android.view.View, 常用的 setText / on
|
|
11
|
+
// 没有体现, 这里做一次断言收口.
|
|
12
|
+
interface TextViewLike {
|
|
13
|
+
setText(text: string): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface ViewLike {
|
|
17
|
+
on(event: string, listener: () => void): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const title = ui.findView("title") as unknown as TextViewLike;
|
|
21
|
+
|
|
22
|
+
const meta = ui.findView("meta") as unknown as TextViewLike;
|
|
23
|
+
meta.setText(`${device.brand} ${device.model} · Android ${device.release}`);
|
|
24
|
+
|
|
25
|
+
const button = ui.findView("btnClose") as unknown as ViewLike;
|
|
26
|
+
button.on("click", () => {
|
|
27
|
+
toast("已退出");
|
|
28
|
+
exit();
|
|
29
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2018",
|
|
4
|
+
"lib": ["ES2018"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "Bundler",
|
|
7
|
+
"types": ["@sm003/autojs6-dts"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"noUnusedLocals": true,
|
|
11
|
+
"noUnusedParameters": true,
|
|
12
|
+
"noImplicitOverride": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"ignoreDeprecations": "6.0",
|
|
18
|
+
"baseUrl": ".",
|
|
19
|
+
"paths": {
|
|
20
|
+
"@/*": ["src/*"]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*.ts", "types/**/*.d.ts"]
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="@sm003/autojs6-dts" />
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AutoJs6 的 CommonJS 模块包装变量.
|
|
5
|
+
* 在 `.ts` 里通常用不到 (源码按 ESM 写, 由打包器转换), 需要时可用于兼容旧脚本.
|
|
6
|
+
*/
|
|
7
|
+
declare const module: { exports: unknown };
|
|
8
|
+
declare const exports: Record<string, unknown>;
|
|
9
|
+
declare const __filename: string;
|
|
10
|
+
declare const __dirname: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `import text from './foo.xml?raw'` — 以字符串形式引入任意静态文件.
|
|
14
|
+
* (没有引入 vite/client, 避免把 DOM 类型混进 Rhino 环境)
|
|
15
|
+
*/
|
|
16
|
+
declare module "*?raw" {
|
|
17
|
+
const content: string;
|
|
18
|
+
export default content;
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { fileURLToPath, URL } from "node:url";
|
|
2
|
+
import { defineConfig } from "vite";
|
|
3
|
+
import { autojs6 } from "vite-plugin-autojs6";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 通过 `--mode` 控制产物形态:
|
|
7
|
+
* development / production 普通脚本
|
|
8
|
+
* development-ui / production-ui UI 模式脚本 (产物首行写入 `"ui";`)
|
|
9
|
+
*/
|
|
10
|
+
export default defineConfig(({ mode }) => {
|
|
11
|
+
const uiMode = mode.includes("ui");
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
// 产物是单文件脚本, 不需要浏览器相关的配置
|
|
15
|
+
appType: "custom" as const,
|
|
16
|
+
resolve: {
|
|
17
|
+
alias: {
|
|
18
|
+
"@": fileURLToPath(new URL("./src", import.meta.url))
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
plugins: [
|
|
22
|
+
autojs6({
|
|
23
|
+
entry: "src/main.ts",
|
|
24
|
+
// UI 模式下改用这个入口
|
|
25
|
+
uiEntry: "src/main.ui.ts",
|
|
26
|
+
uiMode,
|
|
27
|
+
outDir: "dist",
|
|
28
|
+
fileName: "main.js",
|
|
29
|
+
// 随脚本一起部署到 AutoJs6 的静态资源
|
|
30
|
+
assets: ["project.json", "res", "images"],
|
|
31
|
+
// 构建结束自动把 dist 推到手机 (调用 VSCode 里的 AutoJs6 扩展):
|
|
32
|
+
deploy: { enabled: true, command: "save", modes: "all" }
|
|
33
|
+
})
|
|
34
|
+
]
|
|
35
|
+
};
|
|
36
|
+
});
|