这篇文章主要介绍了利用adb shell和node.js实现抖音自动抢红包功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
逻辑很简单,在抖音视频播完之后如果是红包视频,会跳出红包。 我们模拟逻辑如下:
点击屏幕中央,如果有红包打开红包,没有红包则暂停视频。
点击返回按钮,如果有红包关闭红包界面,没有红包提示再按一次退出(其实没退出)。
进行上滑操作,进入下一个视频。
点击、返回、上滑,就这么三步行为,无论有红包没红包都成立,只要计算好时间就行。
代码
下面是一段 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
更多按键对应值可以看 android keyevent 对应的值 通过这些命令我们可以对手机进行一些简单操作啦~
最后
这其实是个很简单的逻辑,但是给我很多启发,以后再有什么简单的操作就可以使用adb和node来重复执行啦~ 最后吐槽下:抖音的红包真的少,昨天刷了3个小时就几毛钱,不够电费的说。就当娱乐吧~
ps:下面介绍下android keyevent 对应的值
android keyevent 中的各个值,在使用adb shell input 的时候用得到。
keycode_unknown=0;keycode_soft_left=1;keycode_soft_right=2;keycode_home=3;keycode_back=4;keycode_call=5;keycode_endcall=6;keycode_0=7;keycode_1=8;keycode_2=9;keycode_3=10;keycode_4=11;keycode_5=12;keycode_6=13;keycode_7=14;keycode_8=15;keycode_9=16;keycode_star=17;keycode_pound=18;keycode_dpad_up=19;keycode_dpad_down=20;keycode_dpad_left=21;keycode_dpad_right=22;keycode_dpad_center=23;keycode_volume_up=24;keycode_volume_down=25;keycode_power=26;keycode_camera=27;keycode_clear=28;keycode_a=29;keycode_b=30;keycode_c=31;keycode_d=32;keycode_e=33;keycode_f=34;keycode_g=35;keycode_h=36;keycode_i=37;keycode_j=38;keycode_k=39;keycode_l=40;keycode_m=41;keycode_n=42;keycode_o=43;keycode_p=44;keycode_q=45;keycode_r=46;keycode_s=47;keycode_t=48;keycode_u=49;keycode_v=50;keycode_w=51;keycode_x=52;keycode_y=53;keycode_z=54;keycode_comma=55;keycode_period=56;keycode_alt_left=57;keycode_alt_right=58;keycode_shift_left=59;keycode_shift_right=60;keycode_tab=61;keycode_space=62;keycode_sym=63;keycode_explorer=64;keycode_envelope=65;keycode_enter=66;keycode_del=67;keycode_grave=68;keycode_minus=69;keycode_equals=70;keycode_left_bracket=71;keycode_right_bracket=72;keycode_backslash=73;keycode_semicolon=74;keycode_apostrophe=75;keycode_slash=76;keycode_at=77;keycode_num=78;keycode_headsethook=79;keycode_focus=80;//*camera*focuskeycode_plus=81;keycode_menu=82;keycode_notification=83;keycode_search=84;keycode_media_play_pause=85;keycode_media_stop=86;keycode_media_next=87;keycode_media_previous=88;keycode_media_rewind=89;keycode_media_fast_forward=90;keycode_mute=91;
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
在immutable.js中如何实现撤销重做功能(详细教程)
在vue中有几种绑定变量的值以及防止其改变的方法(详细教程)
在javascript中定义函数用 var foo = function () {} 和 function foo()区别介绍(详细教程)
以上就是使用node.js实现抖音自动抢红包功能的详细内容。