逻辑很简单,在抖音视频播完之后如果是红包视频,会跳出红包。 我们模拟逻辑如下:
点击屏幕中央,如果有红包打开红包,没有红包则暂停视频。
点击返回按钮,如果有红包关闭红包界面,没有红包提示再按一次退出(其实没退出)。
进行上滑操作,进入下一个视频。
点击、返回、上滑,就这么三步行为,无论有红包没红包都成立,只要计算好时间就行。
代码
下面是一段 node.js 代码:
touch.js
var process = require('child_process');
function exec(shell) {
process.exec(shell,function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
function click() {
console.log('click')
exec(`adb shell input tap 400 600`)
setTimeout(back, 1000)
}
function swipe() {
console.log('swipe')
exec(`adb shell input swipe 400 800 400 0 500`)
setTimeout(click, 20000)
}
function back() {
console.log('back')
exec(`adb shell input keyevent 4`)
setTimeout(swipe, 1000)
}
swipe()打开手机的开发者模式,启动 USB调试 ,如果是小米请另外打开 USB调试(安全设置) 。连接手机,打开抖音主界面。将这个js保存到本地,使用node执行即可。
$ node touch.js如果发现抖音每20秒上滑一次,说明成功啦~
原理
类似使用 adb shell 来操作手机的文章还有操作跳一跳等,下面说下原理。
child_process.exec(command[, options][, callback])该方法功能为衍生一个 shell,然后在 shell 中执行 command,且缓冲任何产生的输出。具体可以看参考文档 其实就是等于执行脚本,shell命令了。 我们利用它来执行 adb shell 命令。
adb shell
adb 是电脑连接手机的开发工具,所有电脑对手机的操作其实都是adb 完成的,包括各种手机助手帮你装 APP 也是。 PS:做了这么久手机,今天才发现这个好玩的功能……汗……
adb shell 可以装apk、看手机信息、操作手机文件、模拟点击行为等功能,是非常强大的。我们这里主要是要模拟点击行为 adb shell input 。 下面罗列下各功能:
// 输入文本 content
$ adb shell input text “hello”
// 点击返回按钮 keynumber
$ adb shell input keyevent 4
// 点击屏幕某个点 x y
$ adb shell input tap 400 400
// 滑动 x1 y1 x2 y2 time
$ adb shell input swipe 400 800 400 0 500
// 下面三个不太清楚,再研究
$ adb shell input press
$ adb shell input roll
$ adb shell input tmode









