The Spring Festival is coming soon, the group will be filled with all kinds of red envelopes, how fast do you grab red envelopes? AutoJS is an Android application software, it can be based on barrier-free services active or task-based, complete a series of automatic operations
Liverpoolfc.tv: pro.autojs.org/
PS: Due to the limitations of AutoJS Pro on mainstream applications, this article is implemented based on AutoJS 4.1.1
Without further ado, let’s go straight to the implementation process
# 1. Prepare and start
First, install VS Code on your PC and AutoJS on your phone
PS: Download package is provided at the end of the article
Then, to ensure that the computer and phone are on the same LAN, AutoJS connects to the computer via IP
Finally, use VS Code to create a JS file and write a script file to open the target application
Var app_name = '**'; // launchApp(app_name) // Keep the screen long device.keepScreenon ()Copy the code
# 2. Search for red packets and click
Defines a method to search for all red packets in the current group chat history
First, get a list of all the red envelope elements by id and class name
Next, the list of red envelope elements is iterated to determine whether the red envelope control is a valid red envelope
Finally, use the click() method to click on the red envelope object to pop up the red envelope snatching dialog box
Var red_package_tag = "u1" function searchNewRedpacket() {className+id, Var rp_msg_list = className(" android.widget.textView ").id(red_package_tag).find(); if (rp_msg_list.length ! = 0) {log(" number of detected red packets: "+ rp_msg_list.length); For (var I = 0; i < rp_msg_list.length; i++) { var rp_u1 = rp_msg_list[i]; Var rp_u1_parent = rp_u1.parent() var rp_u1_parent = rp_u1.parent() = null && rp_u1_parent.childcount () == 1) {log(" find new red money, ready to buy... ); // Click (rp_u1.bounds().centerx (), rp_U1.bounds ().centery ()) log(" Congratulations! Grab a red envelope..." } else {log(" invalid red envelope..." )}}} else if (rp_msg_list.empty()) {// The list of elements is empty log(" no red packet message was detected "+ rp_msg_list.length); } else { return; }}Copy the code
# 3. Grab a red envelope
Define a method for grabbing red packets
PS: Here you need to filter the red packets of expired classes
Function openNewRedPacket() {var draw = desc(" open ").findone (500); if (draw ! = null) { draw.click(); } else {log(" expired "); }}Copy the code
# 4. Loop and exception handling
Define a method to handle the following three types of pages and ensure that you are immediately returned to the chat page when an exception is encountered or an operation is completed
Function back_page() {var red_end = id("f4b"). TextContains (" If (red_end) {log(" red_end ") back()} var red_fp = desc(" If (red_fp) {log(" return 2 ") back()} var red_other = Id (" eyx "). TextContains (" was gone "). FindOnce () if (red_other) {log (" return ") back ()}}Copy the code
# 5. Thread acceleration
There are three methods defined above, and I’m going to put them into three threads for asynchronous execution to improve execution efficiency
Threads.start (function () {while (true) {// Find the red packet and go to searchNewRedpacket()}}); Threads.start (function () {while (true) {openNewRedPacket(); Threads.start (function () {while (true) {back_page()}})Copy the code
# 6. To sum up
Compared with the traditional automatic operation, the addition of multi-thread division of labor operation, can run the automation to grab red packets faster
I have uploaded all the source code in the article to the lower public account background, pay attention to the public account “fried eggs to make money” after the reply keyword “QHB” that can be obtained
If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!