自用的autojs 点击脚本
点击轻松方便,效果大概是这样
传入selector,自动判断是否存在,
限定时间内完成点击,
调用者不必做延时处理,
传入合法控件即可完成点击操作!
function image_module() {
importClass(android.content.Intent);
importClass(android.net.Uri);
importClass(java.io.File);
importClass(java.text.SimpleDateFormat);
importClass(java.util.Calendar);
}
Worker = function(debug) {
this.max_try = 5;
this.debug = typeof(debug) != "undefined" ? debug : true;
this.iton = 1000;
this.path = files.getSdcardPath() + "/脚本/Package" + "/qrcode.png";
this.postion = function(bounds, direction) {
if (direction == "left") add = 150
if (direction == "right") add = -150
if (direction != "center") return bounds[direction] + add
return bounds.centerX()
}
//log(this.path)
}
Worker.prototype.getEle = function(selector) {
//log(selector)
let arrow = selector.findOne(500)
return arrow
}
Worker.prototype.shell = function(target) {
shell("input tap " + target.centerX() + " " + target.centerY())
}
Worker.prototype.log = function(msg, status, type) {
result = {
msg: msg,
status: status
}
if (type && this.debug) console[type](result.msg)
return result
}
Worker.prototype.myclick = function(selector, timeout) {
let obj = null;
if (!timeout) obj = this.getEle(selector);
else {
obj = this.isexists(selector, timeout);
}
if (!obj) return this.log(selector + " 不存在 -getEle", -1, "error")
let first = obj;
for (var i = 0; i < this.max_try; i++) {
if (obj.clickable()) {
obj.click();
sleep(200);
obj = this.getEle(selector);
if (!obj) return this.log(selector + " 依旧存在,可能没点击到 -myclick", 1, "info")
return this.log(" 成功点击 -myclick", 0)
}
obj = obj.parent()
}
result = this.click_pos(first);
if (!result.status > -1 && this.debug) return this.log("成功点击 -myclick", 0)
else return result
}
Worker.prototype.click_pos = function(arrow, postion) {
if (!arrow) return
let bounds = arrow.bounds();
postion = postion || "center"
// log(this.postion(bounds,postion), bounds.centerY())
try {
if (!click(this.postion(bounds, postion), bounds.centerY())) {
return this.log(selector + bounds + " 点击失败 -myclick", -1, "error")
}
} catch (r) {
if (!Tap(this.postion(bounds, postion), bounds.centerY())) {
return this.log(selector + bounds + " 点击失败 -myclick", -1, "error")
}
}
return this.log(" 成功点击 -click_pos", 0)
}
Worker.prototype.isexists = function(selector, timeout) {
timeout = timeout || 5000;
timeout = timeout < 100 ? timeout * 1000 : timeout;
do {
obj = this.getEle(selector);
//log(obj)
if (obj) return obj
sleep(this.iton);
timeout -= this.iton
} while (!obj && timeout > 0);
}
Worker.prototype.storeImg = function(img, path) {
if (!this.img_load) {
this.img_load = 1;
image_module()
}
this.path = path || this.path;
images.save(img, this.path);
return this.scanFile(this.path);
};
Worker.prototype.modify = function(path) {
if (!this.img_load) {
this.img_load = 1;
image_module()
}
return new File(this.path).lastModified()
}
Worker.prototype.scanFile = function(path, type) {
if (!this.img_load) {
this.img_load = 1;
image_module()
}
this.path = path || this.path;
if (type == "delete") files.remove(this.path)
contentUri = Uri.fromFile(File(this.path))
mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri)
app.sendBroadcast(mediaScanIntent)
return this.path;
};
importClass(android.content.Context);
importClass(android.provider.Settings);
/** * 检查自身是否开启某权限 * @param {string} permission 权限名称 */
function check() {
return auto.service
}
Worker.prototype._server = function(max, switches) {
var max = max || 4;
if (max < 2) return
if (switches != 1) {
switches = typeof(switches) == "undefined" ? 1 : 0
}
if (auto.service == null || switches == 0) {
//这里没有启动无障碍服务 这里我启动了autojs免费版和pro版的无障碍服务
try {
var ctn = context.getContentResolver();
var enabledServices = Settings.Secure.getString(ctn, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (switches == 0) {
var Services = "";
} else {
var Services = enabledServices + ":" + context.getPackageName() + "/com.stardust.autojs.core.accessibility.AccessibilityService";
if (add) {
Services += ":" + app.getPackageName(add) + "/com.stardust.autojs.core.accessibility.AccessibilityService";
}
}
Settings.Secure.putString(ctn, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, Services);
Settings.Secure.putString(ctn, Settings.Secure.ACCESSIBILITY_ENABLED, switches);
toastLog("正在" + (switches == "1" ? "打开" : "关闭") + "无障碍服务……")
var cmax = 5
while (!check() && cmax-- > 0) sleep(1500)
} catch (r) {
// shell("pm grant " + context.getPackageName() + " android.permission.WRITE_SECURE_SETTINGS", true)
return this._server(--max)
}
// log("pm grant " + context.getPackageName() + " android.permission.WRITE_SECURE_SETTINGS")
}
if (!check() && switches != 0) {
try {
auto.service.disableSelf();
} catch (r) {
console.warn("无障碍服务有问题");
toastLog("请打开" + app.getAppName(context.getPackageName()) + "的无障碍服务 ")
auto.waitFor();
}
}
}
Worker.prototype.kill = function(appname) {
var name = getPackageName(appname)
app.openAppSetting(name)
this.isexists(textContains("版本"))
// sleep(1000)
const isSure = this.isexists(textMatches(/(.*停.*|.*结.*|.*行.*)/))
if (isSure && isSure.enabled()) {
if (!this.click_pos(isSure)) click(350, 2060)
this.myclick(textMatches(/(.*确.*|.*定.*)/))
toastLog(appname + '应用已被关闭')
sleep(1000)
back()
} else {
toastLog(appname + '应用不能被正常关闭')
back()
}
}
module.exports = Worker; // = Worker;
调用函数
var Clicker = require("./clicker.js")
var Worker # new Clicker(false)
Worker.myclick(text("返回"))
另外,最好的点击办法就是 少点击,有许多方式可以实现这一诉求,感觉最好的一个办法就是,通过intent直接拉出界面(冷启动)。据我观察,营销类app的界面,绝大多数文章界面,商品界面都有快速打开的链接。找intent是一个比较复杂的过程。。但是如果能找到,那么你写出来的脚本稳定性将直线上升!比如: 本文的scheme为 wvhzpj://openlink.cc/c/eyJjIjoiYXBwXzE1NjI5MTYyNDEiLCJkIjp7InVybCI6Imh0dHBzOi8vYmxvZy5jc2RuLm5ldC9MaWx5bGllL2FydGljbGUvZGV0YWlscy8xMDc0MjE3NjQiLCJ1c2VyQWdlbnQiOiJNb3ppbGxhLzUuMCAoTGludXg7IEFuZHJvaWQgMTA7IFJlZG1pIEszMGkgNUcgQnVpbGQvUUtRMS4xOTEyMjIuMDAyOyB3dikgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgVmVyc2lvbi80LjAgQ2hyb21lLzgxLjAuNDA0NC4xMzggTW9iaWxlIFNhZmFyaS81MzcuMzYifSwibSI6Imh1dWZvLTJOUmhrQUFBRnpYdGNjUndESm12dDBaYXNWV0drY0JoanVRcWpBcXF1T2lqUlBQVVBUeERJUXVEUm1ZbXZEelNnSmM3TnIxMVI4MkFJNmRzTGEwSnhuSjVQSkRKSGRtRU5fR21GdEtTTmcxdF9RU0VaR25VazhueDBlWlJnSENBMlc3eUJxbkxXcmhjOFppaUVwY3hpeHpiNG9xak9tbFc1QyJ9
在autojs里面新建文件 写入代码
url = "wvhzpj://openlink.cc/c/eyJjIjoiYXBwXzE1NjI5MTYyNDEiLCJkIjp7InVybCI6Imh0dHBzOi8vYmxvZy5jc2RuLm5ldC9MaWx5bGllL2FydGljbGUvZGV0YWlscy8xMDc0MjE3NjQiLCJ1c2VyQWdlbnQiOiJNb3ppbGxhLzUuMCAoTGludXg7IEFuZHJvaWQgMTA7IFJlZG1pIEszMGkgNUcgQnVpbGQvUUtRMS4xOTEyMjIuMDAyOyB3dikgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgVmVyc2lvbi80LjAgQ2hyb21lLzgxLjAuNDA0NC4xMzggTW9iaWxlIFNhZmFyaS81MzcuMzYifSwibSI6Imh1dWZvLTJOUmhrQUFBRnpYdGNjUndESm12dDBaYXNWV0drY0JoanVRcWpBcXF1T2lqUlBQVVBUeERJUXVEUm1ZbXZEelNnSmM3TnIxMVI4MkFJNmRzTGEwSnhuSjVQSkRKSGRtRU5fR21GdEtTTmcxdF9RU0VaR25VazhueDBlWlJnSENBMlc3eUJxbkxXcmhjOFppaUVwY3hpeHpiNG9xak9tbFc1QyJ9"
app.startActivity({data:url})
//没安装csdn,上述代码,运行后无效果
有哪个app的意图需要抓取,可以在下方留言,留下联系方式。