下载地址:https://www.pan38.com/share.php?code=JCnzE 提取密码:7789
就是用autojs写的一个自动化工具脚本,其实写了好几天,感觉有点价值就分享出来吧
核心代码实现
// 配置参数
const config = {
recordFile: "/sdcard/operationlog" + new Date().getTime() + ".txt",
maxRecordTime: 3600000, // 1小时
sensitivity: 10 // 记录灵敏度
};
// 全局变量
let isRecording = false;
let startTime = 0;
let lastPoint = {x: 0, y: 0};
let operationCount = 0;
// 主记录函数
function startRecording() {
if (isRecording) {
toast("已在记录中");
return;
}
files.create(config.recordFile);
startTime = new Date().getTime();
isRecording = true;
operationCount = 0;
// 写入文件头信息
files.append(config.recordFile,
"AutoJS Operation Log\n" +
"Device: " + device.brand + " " + device.model + "\n" +
"Start Time: " + new Date(startTime).toLocaleString() + "\n" +
"--------------------------------\n"
);
// 注册触摸监听
events.on("touch", function(point) {
if (!isRecording) return;
// 过滤微小移动
if (Math.abs(point.x - lastPoint.x) < config.sensitivity &&
Math.abs(point.y - lastPoint.y) < config.sensitivity) {
return;
}
let timestamp = new Date().getTime() - startTime;
let record = "OP" + (++operationCount).toString().padStart(4, "0") + ": " +
"TIME=" + timestamp + "ms, " +
"X=" + point.x + ", Y=" + point.y + ", " +
"ACTION=" + (point.action || "MOVE") + "\n";
files.append(config.recordFile, record);
lastPoint = {x: point.x, y: point.y};
});
// 自动停止条件检查
threads.start(function() {
while (isRecording &&
(new Date().getTime() - startTime) < config.maxRecordTime) {
sleep(1000);
}
if (isRecording) {
stopRecording();
toast("记录自动停止,已达最大时长");
}
});
}
// 停止记录函数
function stopRecording() {
if (!isRecording) {
toast("未在记录中");
return;
}
let endTime = new Date().getTime();
let duration = (endTime - startTime) / 1000;
files.append(config.recordFile,
"--------------------------------\n" +
"End Time: " + new Date(endTime).toLocaleString() + "\n" +
"Total Duration: " + duration.toFixed(2) + "s\n" +
"Total Operations: " + operationCount + "\n"
);
isRecording = false;
toast("记录已保存: " + config.recordFile);
}
// 回放功能
function replayOperations(logFile) {
if (!files.exists(logFile)) {
toast("日志文件不存在");
return;
}
let content = files.read(logFile);
let lines = content.split("\n");
let startTime = 0;
let lastTime = 0;
for (let line of lines) {
if (!line.startsWith("OP")) continue;
// 解析记录行
let timeMatch = line.match(/TIME=(\d+)ms/);
let xMatch = line.match(/X=(\d+)/);
let yMatch = line.match(/Y=(\d+)/);
let actionMatch = line.match(/ACTION=(\w+)/);
if (!timeMatch || !xMatch || !yMatch) continue;
let currentTime = parseInt(timeMatch[1]);
let x = parseInt(xMatch[1]);
let y = parseInt(yMatch[1]);
let action = actionMatch ? actionMatch[1] : "MOVE";
// 首次记录初始化时间基准
if (startTime === 0) {
startTime = currentTime;
lastTime = currentTime;
}
// 计算等待时间
let waitTime = currentTime - lastTime;
if (waitTime > 0) {
sleep(waitTime);
}
lastTime = currentTime;
// 执行操作
if (action === "DOWN" || action === "MOVE") {
press(x, y, 1);
} else if (action === "UP") {
// 抬起操作
}
}
}
// UI界面
function showMainUI() {
let ui = "ui";
if (typeof ui === "undefined") {
// 命令行模式
console.show();
while (true) {
console.log("请选择操作:");
console.log("1. 开始记录");
console.log("2. 停止记录");
console.log("3. 回放最近记录");
console.log("4. 退出");
let input = console.rawInput();
switch (input) {
case "1":
startRecording();
break;
case "2":
stopRecording();
break;
case "3":
replayOperations(config.recordFile);
break;
case "4":
exit();
default:
console.log("无效输入");
}
}
} else {
// GUI模式
ui.layout(
);
ui.recordBtn.on("click", () => {
startRecording();
ui.recordBtn.enabled = false;
ui.stopBtn.enabled = true;
ui.statusText.text = "状态: 记录中...";
});
ui.stopBtn.on("click", () => {
stopRecording();
ui.recordBtn.enabled = true;
ui.stopBtn.enabled = false;
ui.statusText.text = "状态: 记录已停止";
});
ui.replayBtn.on("click", () => {
if (files.exists(config.recordFile)) {
ui.statusText.text = "状态: 回放中...";
replayOperations(config.recordFile);
ui.statusText.text = "状态: 回放完成";
} else {
toast("没有找到记录文件");
}
});
}
}
// 主程序入口
function main() {
// 申请必要权限
if (!requestScreenCapture()) {
toast("需要屏幕截图权限");
exit();
}
// 显示主界面
showMainUI();
}
// 启动程序
main();
功能扩展建议
增加截图功能:关键操作自动截图保存
添加云端同步:将记录文件自动上传至云端
支持多设备同步:跨设备操作记录合并
开发分析工具:对操作记录进行可视化分析
使用说明
启动脚本后选择"开始记录"
正常操作设备,所有触摸事件将被记录
点击"停止记录"结束记录过程
使用"回放操作"可重现记录的操作流程