autoJs的函数封装
打印日志
js
const Logger = {
// 获取时分秒用于记录日志
logWithTime: function (msg) {
console.info(this.formatTime(new Date(), false) + " " + msg);
},
logWithTimeSSS: function (msg) {
console.info(this.formatTime(new Date(), true) + " " + msg);
},
// 时间戳转时间字符串
formatTime: function (needTime, sss) {
needTime = needTime ? needTime.getTime() : new Date().getTime();
var time = new Date(parseInt(needTime));
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
var S = time.getMilliseconds();
if (sss) {
return (
this.add0(h) +
":" +
this.add0(mm) +
":" +
this.add0(s) +
":" +
this.add0(S)
);
} else {
return this.add0(h) + ":" + this.add0(mm) + ":" + this.add0(s);
}
},
// 补零函数
add0: function (m) {
return m < 10 ? "0" + m : m;
},
};
// 使用示例
Logger.logWithTime("This is a log message.");
Logger.logWithTimeSSS("This is a log message.");
时间格式化
js
/**
* 日期工具类
*/
const DateUtil = {
// 声明一个属性并赋予默认值
defaultFormat: "YYYY/MM/DD HH:mm:ss",
defaultFormat2: "YYYY-MM-DD HH:mm:ss",
defaultFormat3: "YYYY/MM/DD",
//格式化Date
formatDate: function (date, format) {
date = date || new Date();
format = format || this.defaultFormat;
var year = date.getFullYear();
var month = this.padZero(date.getMonth() + 1); // 月份从0开始,需要加1
var day = this.padZero(date.getDate());
var hours = this.padZero(date.getHours());
var minutes = this.padZero(date.getMinutes());
var seconds = this.padZero(date.getSeconds());
let str;
if (format === this.defaultFormat) {
str = `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
} else if (format === this.defaultFormat2) {
str = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
} else if (format === this.defaultFormat3) {
str = `${year}/${month}/${day}`;
}
return str;
},
padZero: function (num) {
return num < 10 ? "0" + num : num;
},
// 判断时间是上午还是下午
getCurrentTimePeriod: function (date) {
const now = date || new Date();
const hours = now.getHours();
if (hours >= 0 && hours < 12) {
return "上午";
} else {
return "下午";
}
},
};
// 使用示例
DateUtil.formatDate(); //2024/11/12 22:56:46
随机滑动
js
for (let i = 0; i < 3; i++) {
MaintenanceAccount(i);
}
function MaintenanceAccount(i) {
let time = random(5000, 12000); //设置滑动延迟时间
toastLog("第" + (i + 1) + "个视频,请等待" + time / 1000 + "秒");
sleep(time);
//开始滑动视频
let x1 = random(device.width / 4, (device.width / 4) * 3);
let y1 = random((device.height / 4) * 3.1, (device.height / 4) * 3.3);
let x2 = random(device.width / 4, (device.width / 4) * 3);
let y2 = random((device.height / 4) * 0.7, (device.height / 4) * 0.5);
let s = random(100, 150); //设置滑动时长
RandomSwipe(x1, y1, x2, y2, s); //向上滑
// RandomSwipe(x2, y2, x1, y1, s);//向下滑
}
function RandomSwipe(qx, qy, zx, zy, time) {
var xxy = [time];
var point = [];
var dx0 = {
x: qx,
y: qy,
};
var dx1 = {
x: random(qx - (device.width / 4) * 0.25, qx + (device.width / 4) * 0.25),
y: random(qy, qy + 50),
};
var dx2 = {
x: random(zx - (device.width / 4) * 0.25, zx + (device.width / 4) * 0.25),
y: random(zy, zy + 50),
};
var dx3 = {
x: zx,
y: zy,
};
for (let i = 0; i < 4; i++) {
eval("point.push(dx" + i + ")");
}
for (let i = 0; i < 1; i += 0.08) {
xxyy = [
parseInt(bezier_curves(point, i).x),
parseInt(bezier_curves(point, i).y),
];
xxy.push(xxyy);
}
gesture.apply(null, xxy);
}
function bezier_curves(cp, t) {
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: 0,
y: 0,
};
result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
return result;
}
随机点击
js
/**
* 随机点击函数
* @param {*} 控件对象
*/
function randomclick(控件对象) {
let bound = 控件对象.bounds();
let randX = bound.left + random(1, bound.right - bound.left);
let randY = bound.top + random(1, bound.bottom - bound.top);
click(randX, randY);
}
保存图片到本地
js
var res = http.get("https://blog.share888.top/logo.png");
log("code = " + res.statusCode);
files.writeBytes("/sdcard/脚本/123.jpg", res.body.bytes()); //网络图片获取到手机