共计 3569 个字符,预计需要花费 9 分钟才能阅读完成。
hello,大家好,我是帅气的超级饭饭团,最近刚和大舅子约了个晚饭,饭桌上大舅子第一句话便是:
据说你是程序员?能够帮我写个自动化脚本吗?
我发现外行人都感觉程序员是万能的啊,然而其实咱们都只是会 ctrl c + ctrl v 而已
不过,大舅子提需要了,能认怂吗?
当然不能啊,必须做啊
尽管我连自动化脚本怎么做、用什么语言都不晓得,然而,为了妹子,不能怂啊
我喝了一两白酒,壮了壮胆子,和大舅子探讨了下需要
酒过三巡菜过五味,最初终于将需要定了下来了
大略是这样的:
- 主动登录某 app
- 主动搜寻指定宝贝
- 主动评论留言
- 自定义搜寻关键词、自定义留言个数
总结下来就是依据关键词主动评论,而后还要提供一个 UI 操作
确定了需要了,加上白红啤怼了几杯后,我反诘大舅子那你妹就是我的了吧
大舅子说:需要搞进去,她就是你的了
为了妹子,必须拼啊,当晚开始奋斗
先是搜寻相干技术,而后开始查漏补缺,最初便是开始实现
给大家看看须要的技术:
- autojs,自动化脚本引擎
- 一点 js 编程意识,实现语言用的就是 js
- 一点安卓意识,毕竟自动化脚本针对的对象就是安卓手机了
最初给大家看看我奋笔疾书一整晚的成果
大略分为几步:
第一步,UI 界面的设计
ui.layout( | |
<vertical> | |
<text textSize="18sp" textColor="#000000" margin="20" textStyle="bold"> | |
闲鱼主动评论 | |
</text> | |
<ScrollView> | |
<vertical> | |
<text textSize="16sp" margin="8">1. 宝贝标签 </text> | |
<input w="*" text="卫衣" id="target" margin="0 16" /> | |
<text textSize="16sp" margin="8">2. 评论内容 </text> | |
<input w="*" text="你好" id="comment" margin="0 16" /> | |
<text textSize="16sp" margin="8">3. 总解决条数 </text> | |
<input text="2" id="total" inputType="number" margin="0 16" /> | |
<linear gravity="center"> | |
<button margin="16" id="ok"> 开始执行 </button> | |
</linear> | |
</vertical> | |
</ScrollView> | |
</vertical> | |
) |
最终出现进去的界面是这样的
尽管丑,然而能用就能够啦
第二步便是点击后的解决
ui.ok.click(() => {var target = ui.target.text(); | |
var comment = ui.comment.text(); | |
var total = ui.total.text(); | |
let main = new Main(); | |
threads.start(function () {main.process(target, comment, total); | |
}); | |
}); |
也就是监听了按钮的点击解决
第三步就是真正做自动化的中央了
let utils = new AppUtils(); | |
utils.consoleShow(); | |
console.log("解决的参数:" + target + "," + message + "," + limit); | |
let mainTarget; | |
auto.waitFor(); | |
utils.openApp("闲鱼"); | |
let search = id("search_bar_layout").untilFind(); | |
utils.clickView(search[0]); | |
let sousuoInputs = className("android.widget.EditText").indexInParent(1).depth(6).untilFind() | |
sousuoInputs[0].setText(target) | |
utils.paste(sousuoInputs[0], target); | |
utils.sleep(1000); | |
let results = className("android.view.View").descContains(target).untilFind(); | |
utils.clickView(results[0]); | |
mainTarget = results[0].desc(); | |
console.log("设置标签:" + mainTarget); | |
var targetViewMap = new java.util.HashMap(); | |
while (targetViewMap.size() < limit) { | |
let viewIndex = 0; | |
while (true) {let targetViews = className("android.view.View").descContains(target).untilFind().filter(function (w) {return w.desc().length >= 10; | |
}); | |
if (targetViews.length <= viewIndex) {break;} | |
let targetView = targetViews[viewIndex++]; | |
let text = targetView.desc(); | |
text = text.substring(0, Math.min(10, text.length)); | |
if (!targetViewMap.containsKey(text)) {utils.sleep(1000); | |
utils.clickView(targetView); | |
utils.sleep(1000); | |
if (textContains("客服").findOnce() != null) {utils.tryback(mainTarget); | |
continue; | |
} | |
let btns = className("android.view.View").untilFind() | |
var leaveMessage; | |
for (key in btns) {let btn = btns[key] | |
try {if (btn.desc() == null) {continue;} | |
if (btn.desc() == "留言") { | |
leaveMessage = btn; | |
break; | |
} | |
if (!isNaN(btn.desc())) { | |
leaveMessage = btn; | |
break; | |
} | |
} catch (error) {}} | |
utils.clickView(leaveMessage); | |
utils.sleep(1000); | |
try{leaveMessage.setText(message) | |
} catch(error) { } | |
try {let leaveMessage2 = descContains("看对眼就留言").findOnce(); | |
utils.clickView(leaveMessage2); | |
utils.sleep(1000); | |
leaveMessage2.setText(message) | |
} catch (error) { } | |
try {let leaveMessage3 = textContains("看对眼就留言").findOnce(); | |
utils.clickView(leaveMessage3); | |
utils.sleep(1000); | |
leaveMessage3.setText(message) | |
} catch (error) { } | |
utils.sleep(1000); | |
let sendBtn = textContains("发送").findOnce(); | |
if (sendBtn == null) {console.warn("找不到发送按钮:" + text) | |
continue; | |
} | |
utils.clickView(sendBtn); | |
console.log("评论胜利,防止被监控,进行 1 秒") | |
utils.tryback(mainTarget); | |
utils.sleep(3000); | |
targetViewMap.put(text, targetView); | |
console.log("以后胜利评论个数:" + targetViewMap.size()) | |
if (targetViewMap.size() >= limit) {break;} | |
} | |
} | |
while (true) {if (scrollDown(0)) {break;} | |
} | |
} | |
console.log("执行结束"); |
能够看到第 3 步挺多 sleep 的中央,都是为了期待界面渲染的工夫,因为手机比拟卡,所以期待的工夫也绝对长了些。
最初将脚本打包后发给了大舅子,大舅子叫苦不迭 …….
当然啦,大舅子开心,我也开心,毕竟抱得妹子归啊哈哈哈哈
另外,我将自动化学习教程和实战例子都放到 github 和码云上了,有趣味学习的能够拿去看
github 地址:脚本仓库
码云地址:脚本仓库
b 站视频:教学视频
正文完
发表至: javascript
2021-11-15