I am a handsome super rice ball. I just had a dinner with my brother in law recently. The first thing he said at the dinner table was:

I heard you’re a programmer? Can you write an automated script for me?

I find that laymen think programmers can do everything, but all we really know is CTRL C + CTRL V

But will you accept my brother in law’s request?

Of course not. It has to be done

I don’t even know how to do an automated script or what language to use, but, for a girl, why not

I had a drink or two of white wine and, emboldened, discussed my needs with my brother-in-law

After three rounds of wine and five dishes, the demand was finally settled

It goes something like this:

  • Automatically log in to an app
  • Automatically searches for specified treasures
  • Automatic comment message
  • You can customize the search keywords and the number of messages

This boils down to automatic reviews based on keywords and then providing a UI operation

Determined the demand, and white red beer after a few drinks, I asked my brother-in-law that your sister is mine

My brother in law said: If you need something, she is yours

You got to fight for the girl, and you got to fight that night

First you search for the technology, then you start filling in the gaps, and finally you start implementing it

To show you the techniques needed:

  • Autojs, automatic scripting engine
  • A little JS programming awareness, the implementation of the language is JS
  • A little Android awareness, after all, automated scripts are targeted at Android phones

And finally, I’ll show you what happens when I write all night long

It can be divided into several steps:

The first step is the design of UI interface

ui.layout(
    <vertical>
        <text textSize="18sp" textColor="# 000000" margin="20" textStyle="bold">Idle fish automatic comments</text>
        <ScrollView>
            <vertical>
                <text textSize="16sp" margin="8">1. Baby labels</text>
                <input w="*" text="Fleece" id="target" margin=16 "0" />
                <text textSize="16sp" margin="8">2. Comment content</text>
                <input w="*" text="Hello" id="comment" margin=16 "0" />
                <text textSize="16sp" margin="8">3. Total number of processing items</text>
                <input text="2" id="total" inputType="number" margin=16 "0" />
                <linear gravity="center">
                    <button margin="16" id="ok">Start to perform</button>
                </linear>
            </vertical>
        </ScrollView>
    </vertical>
)
Copy the code

The resulting interface looks like this

It’s ugly, but it works

The second step is post-click processing

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);
    });
});
Copy the code

That’s listening for button clicks

The third step is where the automation really comes in

let utils = new AppUtils();
        utils.consoleShow();
        console.log("Processing parameters:" + target + "," + message + "," + limit);
        let mainTarget;
        auto.waitFor();
        utils.openApp("Idle fish");

        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("Set tag:" + 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("Customer service").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() == "Message") {
                                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("Leave a message if you see a match.").findOnce();
                        utils.clickView(leaveMessage2);
                        utils.sleep(1000);
                        leaveMessage2.setText(message)
                    } catch (error) {
                    }
                    try {
                        let leaveMessage3 = textContains("Leave a message if you see a match.").findOnce();
                        utils.clickView(leaveMessage3);
                        utils.sleep(1000);
                        leaveMessage3.setText(message)
                    } catch (error) {
                    }
                 
                    utils.sleep(1000);
                    let sendBtn = textContains("Send").findOnce();
                    if (sendBtn == null) {
                        console.warn("Can't find send button:" + text)
                        continue;
                    }
                    utils.clickView(sendBtn);
                    console.log("Comment succeeded, avoid being monitored, stop for 1 second.")
                    utils.tryback(mainTarget);
                    utils.sleep(3000);

                    targetViewMap.put(text, targetView);
                    console.log("Current number of successful comments:" + targetViewMap.size())

                    if (targetViewMap.size() >= limit) {
                        break; }}}while (true) {
                if (scrollDown(0)) {
                    break; }}}console.log("Executed");
Copy the code

It can be seen that there is a lot of sleep in the third step, which is to wait for the interface rendering time, because the phone is relatively card, so the waiting time is relatively long.

Finally, the script was packed and sent to my brother-in-law, who was overjoyed…….

Of course, my brother-in-law is happy, I am also happy, after all, hold the sister return ah-ha-ha-ha

In addition, I have put the automatic learning tutorial and practical examples on Github and code cloud, those who are interested in learning can take a look

Github address: script repository

Code cloud address: script repository

Station B video: teaching video