// 配置参数(根据实际图片路径和需求修改)
const TEMPLATE_PATH = "/sdcard/hamibot/templates/"; // 模板图片目录(Hamibot常用路径)
const BUY_BTN = TEMPLATE_PATH + "buy_btn.png"; // 立即选购按钮
const SUBMIT_BTN = TEMPLATE_PATH + "submit_btn.png"; // 提交按钮
const SIZE_PREFIX = TEMPLATE_PATH + "size_"; // 尺码模板前缀(如size_36.png)
const SIZES = ["36", "37", "38", "39", "40", "41", "42", "43"]; // 需选择的尺码
const THRESHOLD = 0.8; // 匹配阈值(可根据识别效果调整)
const WAIT_TIMEOUT = 15000; // 等待元素超时时间(毫秒)
// 等待目标图片出现并返回位置(使用Hamibot的findImage方法)
function waitForImage(template, timeout) {
timeout = timeout || WAIT_TIMEOUT;
const start = Date.now();
while (Date.now() - start < timeout) {
console.log(检测目标图片:${template});
// 使用Hamibot的图像识别API
const pos = findImage(captureScreen(), template, {
threshold: THRESHOLD
});
if (pos) {
return pos;
}
sleep(500); // 等待500ms后重试
}
console.error(超时未找到目标:${template});
return null;
}
// 点击目标图片中心(使用Hamibot的click方法)
function clickImage(template) {
const pos = waitForImage(template);
if (!pos) return false;
// 计算中心坐标并点击
const x = pos.x + pos.width / 2;
const y = pos.y + pos.height / 2;
click(x, y);
console.log(点击图片中心:${template} (${x}, ${y}));
sleep(1000); // 等待界面响应
return true;
}
// 选择指定尺码(循环检测并点击36-43码)
function selectSizes() {
for (let size of SIZES) {
const sizeTemplate = SIZE_PREFIX + size + ".png";
console.log(尝试选择${size}码...);
if (!clickImage(sizeTemplate)) {
console.warn(未找到${size}码,跳过);
}
}
}
// 主流程
function main() {
console.log("开始自动化流程...");
// 1. 点击“立即选购”按钮
console.log("查找并点击【立即选购】...");
if (!clickImage(BUY_BTN)) {
console.error("无法进入商品详情,终止脚本");
return;
}
// 2. 选择36-43码
console.log("进入尺码选择界面,开始选择尺码...");
selectSizes();
// 3. 点击提交/确认按钮
console.log("查找并点击【提交】...");
if (clickImage(SUBMIT_BTN)) {
console.log("操作完成!");
} else {
console.error("提交失败");
}
}
// 启动脚本并捕获异常
try {
main();
} catch (e) {
console.error("脚本出错:", e.message);
}
回答
你的脚本整体结构清晰,但需要注意以下几点:
-
函数调用的正确性:
findImage是 Hamibot 中用于图像识别的函数,但需要确保你使用的 Hamibot 版本支持该函数。如果使用的是较旧版本,可能需要使用images.findImage或其他相关函数。captureScreen()是用于截屏的函数,确保它在当前环境中可用。
-
代码兼容性:
- 由于 Hamibot 使用的是 Rhino 1.7.15 JavaScript 引擎,不支持箭头函数(
=>)和某些现代 JavaScript 特性。你需要将代码中的箭头函数替换为传统函数表达式。 - 在脚本开头添加注释:
// 文档地址 https://docs.hamibot.com/,以确保兼容性。
- 由于 Hamibot 使用的是 Rhino 1.7.15 JavaScript 引擎,不支持箭头函数(
-
错误处理:
waitForImage函数中,findImage的参数可能需要调整,确保传入正确的参数。例如,findImage(captureScreen(), template, { threshold: THRESHOLD })可能需要根据实际 API 调整。
-
路径问题:
- 确保
TEMPLATE_PATH和其他图片路径正确,并且图片已上传到设备的指定目录。
- 确保
以下是修改后的代码示例:
// 文档地址 https://docs.hamibot.com/
// 配置参数(根据实际图片路径和需求修改)
const TEMPLATE_PATH = "/sdcard/hamibot/templates/"; // 模板图片目录(Hamibot常用路径)
const BUY_BTN = TEMPLATE_PATH + "buy_btn.png"; // 立即选购按钮
const SUBMIT_BTN = TEMPLATE_PATH + "submit_btn.png"; // 提交按钮
const SIZE_PREFIX = TEMPLATE_PATH + "size_"; // 尺码模板前缀(如size_36.png)
const SIZES = ["36", "37", "38", "39", "40", "41", "42", "43"]; // 需选择的尺码
const THRESHOLD = 0.8; // 匹配阈值(可根据识别效果调整)
const WAIT_TIMEOUT = 15000; // 等待元素超时时间(毫秒)
// 等待目标图片出现并返回位置(使用Hamibot的findImage方法)
function waitForImage(template, timeout) {
timeout = timeout || WAIT_TIMEOUT;
var start = Date.now();
while (Date.now() - start < timeout) {
console.log("检测目标图片:" + template);
// 使用Hamibot的图像识别API
var pos = findImage(captureScreen(), template, {
threshold: THRESHOLD
});
if (pos) {
return pos;
}
sleep(500); // 等待500ms后重试
}
console.error("超时未找到目标:" + template);
return null;
}
// 点击目标图片中心(使用Hamibot的click方法)
function clickImage(template) {
var pos = waitForImage(template);
if (!pos) return false;
// 计算中心坐标并点击
var x = pos.x + pos.width / 2;
var y = pos.y + pos.height / 2;
click(x, y);
console.log("点击图片中心:" + template + " (" + x + ", " + y + ")");
sleep(1000); // 等待界面响应
return true;
}
// 选择指定尺码(循环检测并点击36-43码)
function selectSizes() {
for (var size in SIZES) {
var sizeTemplate = SIZE_PREFIX + SIZES[size] + ".png";
console.log("尝试选择" + SIZES[size] + "码...");
if (!clickImage(sizeTemplate)) {
console.warn("未找到" + SIZES[size] + "码,跳过");
}
}
}
// 主流程
function main() {
console.log("开始自动化流程...");
// 1. 点击“立即选购”按钮
console.log("查找并点击【立即选购】...");
if (!clickImage(BUY_BTN)) {
console.error("无法进入商品详情,终止脚本");
return;
}
// 2. 选择36-43码
console.log("进入尺码选择界面,开始选择尺码...");
selectSizes();
// 3. 点击提交/确认按钮
console.log("查找并点击【提交】...");
if (clickImage(SUBMIT_BTN)) {
console.log("操作完成!");
} else {