It's almost the new year. How to use AutoJS automation to quickly grab wechat red envelopes!

The new year is coming. The group is usually full of all kinds of red envelopes. How fast do you grab red envelopes? AutoJS is an Android application software, which can complete a series of automatic operations based on barrier free service initiative or task-based

Official website: https://pro.autojs.org/

PS: due to the limitations of AutoJS Pro on mainstream applications, this article is implemented based on AutoJS 4.1.1

No more nonsense. Let's talk about the implementation process directly

# 1. Preparation and start-up

First, install VS Code on PC and AutoJS application on mobile phone

PS: download package is provided at the end of the article

Then, ensure that the computer and mobile phone are on the same LAN, and AutoJS connects the computer through ip

Finally, create a JS file using VS Code and write a script file to open the target application

//Open target app
//apply name
var app_name = '**';

//Open app
launchApp(app_name)

//Keep the screen on
device.keepScreenOn()

# 2. Search red envelope and click

Define a method to search all red envelope records in the current group chat record

First, get the list of all red envelope elements by id and class name

Then, traverse the list of red envelope elements to determine whether the red envelope control is a valid red envelope

Finally, click the red envelope object with the {click() method to pop up the red envelope grabbing dialog box

//Description information of each red envelope (input by yourself)
var red_package_tag = "u1"

function searchNewRedpacket() {
    //Find elements together through className+id
    var rp_msg_list = className("android.widget.TextView").id(red_package_tag).find();
    if (rp_msg_list.length != 0) {
        log("Number of red packets detected: " + rp_msg_list.length);

        //Traversing red envelope objects
        for (var i = 0; i < rp_msg_list.length; i++) {
            var rp_u1 = rp_msg_list[i];
            var rp_u1_parent = rp_u1.parent()
            // Judge whether it is a valid red envelope
            if (rp_u1_parent != null && rp_u1_parent.childCount() == 1) {
                log("Find a new red envelope and prepare to rob...");
                //Click through the coordinates of the element
                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 element list is empty
        log("No red envelope message detected" + rp_msg_list.length);
    } else {
        return;
    }
}

# 3. Grab a red envelope

Define a method to grab red envelopes

PS: you need to filter the red packets of expired classes here

//Open the red envelope and rob
function openNewRedPacket() {
    //Find the element by desc description
    var draw = desc("open").findOne(500);
    if (draw != null) {
        draw.click();
    } else {
        log("Invalid red envelopes such as expired");
    }
}

# 4. Loop and exception handling

Define a method to handle the following three pages to ensure that you return to the chat page immediately after an exception or operation is completed

//Return to previous page
function back_page() {
    //Red envelope robbed (dialog box)
    var red_end = id("f4b").textContains("The hand is slow and the red envelope pie is over").findOnce()
    if (red_end) {
        log("Return one")
        back()
    }
    //Red envelope receiving page - grab it yourself
    var red_fp = desc("The change has been deposited and can be withdrawn directly").findOnce()
    if (red_fp) {
        log("Return two")
        back()
    }

    //Red envelope receiving page -- robbed by others
    var red_other = id("eyx").textContains("Robbed").findOnce()
    if (red_other) {
        log("Return three")
        back()
    }
}

# 5. Thread acceleration

Three methods are defined above. I put them into three threads to execute asynchronously to improve execution efficiency

//Thread 1: click red envelope to enter
threads.start(function () {
    //Code executed on a new thread (child thread)
    while (true) {
        //Look for red envelopes and enter
        searchNewRedpacket()
    }
});

//Thread 2: click the [on] button)
threads.start(function () {
    while (true) {
        openNewRedPacket();
    }
})

//Thread 3: if the red envelope is robbed, it will return immediately
threads.start(function () {
    while (true) {
        back_page()
    }
})

# 6. To sum up

Compared with the traditional automation operation, adding multi-threaded division of labor operation can run automation faster to grab red envelopes

Official account of the official "fried eggs" is published in the background of the official account. I can get the reply "qhb" after the "code".

If you think the article is good, please , like, share and leave a message , because this will be the strongest driving force for me to continue to output more high-quality articles!

Keywords: autojs

Added by RoBoTTo on Wed, 05 Jan 2022 20:07:44 +0200