// 确保环境中有device对象(Hamibot特有)
if (typeof device === 'undefined') {
console.error("device对象未定义!请在Hamibot环境中运行此脚本");
exit();
}
// 定义random函数(Hamibot环境中可能不存在)
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 定义log函数(兼容Hamibot和普通JS)
function log(message) {
if (typeof console !== 'undefined') {
console.log(message);
}
}
// 定义sleep函数(Hamibot环境中已存在,但这里做兼容)
if (typeof sleep === 'undefined') {
function sleep(ms) {
const start = new Date().getTime();
while (new Date().getTime() - start < ms) {}
}
}
function getBezierPath(p1, p2, p3, p4, times) {
function Point2D(x, y) {
this.x = x || 0.0;
this.y = y || 0.0;
}
function PointOnCubicBezier(cp, t) {
var ax, bx, cx;
var ay, by, cy;
var tSquared, tCubed;
var result = new Point2D();
cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx;
cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by;
tSquared = t * t;
tCubed = tSquared * t;
result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
return result;
}
function ComputeBezier(cp, numberOfPoints, curve) {
var dt = 1.0 / (numberOfPoints - 1);
for (var i = 0; i < numberOfPoints; i++) {
curve[i] = PointOnCubicBezier(cp, i * dt);
}
}
// 修复点顺序问题:起点->控制点1->控制点2->终点
var cp = [
new Point2D(parseInt(p1[0]), parseInt(p1[1])),
new Point2D(parseInt(p2[0]), parseInt(p2[1])),
new Point2D(parseInt(p3[0]), parseInt(p3[1])),
new Point2D(parseInt(p4[0]), parseInt(p4[1])),
];
var curve = [];
ComputeBezier(cp, times, curve);
return curve;
}
// 从下往上滑动(修复版)
function swipeTop(dw, dh, duration) {
// 随机滑动时间(300-600毫秒)
if (!(typeof duration === 'number' && !isNaN(duration) && duration > 0)) {
duration = random(300, 600);
}
// 确保坐标在屏幕范围内
const safeX = (value) => Math.max(50, Math.min(dw - 50, value));
const safeY = (value) => Math.max(50, Math.min(dh - 50, value));
// 起点(底部区域)
const startX = safeX(random(dw * 0.2, dw * 0.8));
const startY = safeY(random(dh * 0.7, dh * 0.9));
// 终点(顶部区域)
const endX = safeX(random(dw * 0.2, dw * 0.8));
const endY = safeY(random(dh * 0.1, dh * 0.3));
// 控制点(增加曲线效果)
const controlRange = Math.min(dw, dh) * 0.2;
const ctl1 = [
safeX(startX + random(-controlRange, controlRange)),
safeY(startY + random(-controlRange * 0.5, controlRange * 0.5))
];
const ctl2 = [
safeX(endX + random(-controlRange, controlRange)),
safeY(endY + random(-controlRange * 0.5, controlRange * 0.5))
];
// 生成贝塞尔曲线路径
const path = getBezierPath(
[startX, startY],
ctl1,
ctl2,
[endX, endY],
100 // 路径点数
);
// 准备手势参数
const gestureParams = [duration];
for (let i = 0; i < path.length; i++) {
gestureParams.push([Math.round(path[i].x), Math.round(path[i].y)]);
}
// 执行手势
gesture.apply(null, gestureParams);
}
// 主循环执行(修复版)
function main() {
// 设置循环次数 (0=无限循环)
const MAX_LOOPS = 0;
// 设置滑动间隔范围 (秒)
const INTERVAL_MIN = 3;
const INTERVAL_MAX = 10;
let loopCount = 0;
log("脚本启动,设备分辨率: " + device.width + "x" + device.height);
while (MAX_LOOPS === 0 || loopCount < MAX_LOOPS) {
try {
log("执行滑动 #" + (loopCount + 1));
// 执行滑动
swipeTop(device.width, device.height);
// 随机等待间隔
const waitTime = random(INTERVAL_MIN, INTERVAL_MAX);
log("下次滑动等待: " + waitTime.toFixed(1) + "秒");
// 添加进度提示
const dots = ".".repeat(loopCount % 4 + 1);
log("等待中" + dots);
sleep(waitTime * 1000);
loopCount++;
} catch (e) {
log("执行出错: " + e);
sleep(3000); // 出错后等待3秒再重试
}
}
log("滑动任务完成,共执行 " + loopCount + " 次滑动");
}
// 启动主程序
main();
将此脚本增加运行显示功能,
回答
// 文档地址 https://docs.hamibot.com/
// 确保环境中有device对象(Hamibot特有)
if (typeof device === 'undefined') {
console.error("device对象未定义!请在Hamibot环境中运行此脚本");
exit();
}
// 定义random函数(Hamibot环境中可能不存在)
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 定义log函数(兼容Hamibot和普通JS)
function log(message) {
if (typeof console !== 'undefined') {
console.log(message);
}
}
// 定义sleep函数(Hamibot环境中已存在,但这里做兼容)
if (typeof sleep === 'undefined') {
function sleep(ms) {
const start = new Date().getTime();
while (new Date().getTime() - start < ms) {}
}
}
function getBezierPath(p1, p2, p3, p4, times) {
function Point2D(x, y) {
this.x = x || 0.0;
this.y = y || 0.0;
}
function PointOnCubicBezier(cp, t) {
var ax, bx, cx;
var ay, by, cy;
var tSquared, tCubed;
var result = new Point2D();
cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx;
cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by;
tSquared = t * t;
tCubed = tSquared * t;
result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
return result;
}
function ComputeBezier(cp, numberOfPoints, curve) {
var dt = 1.0 / (numberOfPoints - 1);
for (var i = 0; i < numberOfPoints; i++) {
curve[i] = PointOnCubicBezier(cp, i * dt);
}
}
var cp = [
new Point2D(parseInt(p1[0]), parseInt(p1[1])),
new Point2D(parseInt(p2[0]), parseInt(p2[1])),
new Point2D(parseInt(p3[0]), parseInt(p3[1])),
new Point2D(parseInt(p4[0]), parseInt(p4[1])),
];
var curve = [];
ComputeBezier(cp, times, curve);
return curve;
}
// 从下往上滑动(运行显示增强版)
function swipeTop(dw, dh, duration) {
// 随机滑动时间(300-600毫秒)
if (!(typeof duration === 'number' && !isNaN(duration) && duration > 0)) {
duration = random(300, 600);
}
// 确保坐标在屏幕范围内
const safeX = function(value) { return Math.max(50, Math.min(dw - 50, value)); };
const safeY = function(value) { return Math.max(50, Math.min(dh - 50, value)); };
// 起点(底部区域)
const startX = safeX(random(dw * 0.2, dw * 0.8));
const startY = safeY(random(dh * 0.7, dh * 0.9));
// 终点(顶部区域)
const endX = safeX(random(dw * 0.2, dw * 0.8));
const endY = safeY(random(dh * 0.1, dh * 0.3));
// 控制点(增加曲线效果)
const controlRange = Math.min(dw, dh) * 0.2;
const ctl1 = [
safeX(startX + random(-controlRange, controlRange)),
safeY(startY + random(-controlRange * 0.5, controlRange * 0.5))
];
const ctl2 = [
safeX(endX + random(-controlRange, controlRange)),
safeY(end