Auto.Js 抖音脚本 性别识别 消息自定义 免ROOT

本文相关关键词:
脚本,自动化

适配抖音 8.0.0
仅用于学习使用

抖音私信.js(执行逻辑代码)

(function InitScript() {
    // 监控多脚本运行情况,防干扰
    let c = engines.all().length;
    if (c > 2) {
        toastLog("脚本引擎:发现多个脚本同时运行,即将杀死所有脚本,请重新运行本脚本!");
        engines.stopAll();
    } else {
        const StartToast = "抖音私信脚本已开始,首次运行需要授权截屏权限";
        device.keepScreenOn();
        images.requestScreenCapture(false);
        toast(StartToast);
    }
})();

// *********初始化全局变量************
TAG = "DYSX-> ";
SAVED = storages.create("dyzs");
COUNT = 0;

// 半通用代码 错误打印与退出脚本
function ErrorHandle(Msg, suicide) {
    console.setSize(device.width / 2, device.height);
    console.show();
    console.error(Msg);
    if (suicide) {
        threads.shutDownAll();
        engines.stopAll();
    }
    runtime.sleep(5000);
    console.hide();
};

function clickItem(item) {
    if (!item) {
        return false
    }
    item.click();
    let area = item.bounds();
    let x = area.centerX();
    let y = area.centerY();
    try {
        // Android 7.0  以上的点击
        click(x, y);
    } catch (e) {
        // Root 点击
        try {
            // 容易出毛病,toomanyevenliserException
            Tap(x, y);
            runtime.sleep(1000);
        } catch (e) {
            try {
                var ra = new RootAutomator();
                // 保险点击
                ra.tap(x, y, 1);
                runtime.sleep(800);
                ra.press(x, y, 10, 1);
                ra.exit();
            } catch (e) {
                ErrorHandle("在此设备上需要Root权限才能运行本脚本!", true);
            }

        }
    }
}

function clickHelper(item, is_auto, is_root, is_ge_sdk23) {
    if (!item) {
        return false
    }
    if (is_auto) {
        item.click();
        return
    }

    let area = item.bounds();
    let x = area.centerX();
    let y = area.centerY();

    if (is_ge_sdk23) {
        // Android 7.0  以上的点击
        click(x, y);
    }

    if (is_root) {
        try {
            // 容易出毛病,toomanyevenliserException
            Tap(x, y);
            runtime.sleep(1000);
        } catch (e) {
            try {
                var ra = new RootAutomator();
                // 保险点击
                ra.tap(x, y, 1);
                runtime.sleep(800);
                ra.press(x, y, 10, 1);
                ra.exit();
            } catch (e) {
                ErrorHandle("在此设备上需要Root权限才能运行本脚本!", true);
            }

        }
    }

}


function getGender() {
    function genderRecog(text_) {
        // 预备性别校验(根据文本)
        let gender = "未知";
        const p1 = new RegExp("男");
        const p2 = new RegExp("女");
        if (p1.test(text_)) {
            gender = "男";
        } else if (p2.test(text_)) {
            gender = "女";
        } else {
            function warpGenderRec() {
                let W_gender = null;
                if (textEndsWith("岁").findOne(2000)) {
                    Z = textEndsWith("岁").findOne().bounds();
                    console.hide();
                    ox = Z.left + device.width / 1080 * 23;
                    oy = Z.top + device.height / 1920 * 32;
                    console.log(TAG + "定位颜色点坐标X Y为:", ox, oy);
                    colorValue = images.pixel(images.captureScreen(), ox, oy);
                    console.info(TAG + "colorValueAbs: " + colorValue);
                    if (colorValue + 16211275 < 2000000) {
                        W_gender = "男";
                    } else if (colorValue + 2537115 < 2000000) {
                        W_gender = "女";
                    }
                }
                return W_gender
            }
            // 执行函数
            gender = warpGenderRec();
        }
        return gender
    }
    try {
        msgUi = id("d4z").findOne(4000).children();
    } catch (e) {
        if (text("男").findOne(3000)) { return "男" } else return genderRecog("");
    }
    // toastLog(msgUi);
    runtime.sleep(3000);
    let msgUiSize = msgUi.size();
    msg_ = "";
    for (let i = 0; i < msgUiSize; i++) {
        let text_ = msgUi.get(i).text();
        msg_ += " | " + text_;

    }
    console.verbose(msg_);
    gender = genderRecog(msg_);
    return gender
}

function SendMsg() {
    if (id("abe").findOne(1000)) {
        toastLog(TAG + "企业用户,返回继续下一个");
        return false
    }
    // 进行性别识别
    if (getGender() != "男") {
        toastLog(TAG + "不符合性别条件,返回继续");
        return false
    } else {
        toastLog(TAG + "符合性别条件,开始进行消息发送")
    };

    if (text("关注").findOne(4000)) {
        if (!text("关注").findOne(2000).click()) {
            b = id("cac").findOne(2000);
            // console.log(a.bounds().top, a.bounds().left);
            // console.log(b.bounds().top, b.bounds().left);
            //670,466
            if (b) {
                click(b.bounds().left - 260, b.bounds().top + 46);
            }
        }
        toastLog(TAG + "点击关注");
        runtime.sleep(1500);
        if (!id("cst").findOne(2000)) {
            console.show();
            console.error(TAG + "关注失败1:");
            // console.log(id("cac").findOne(1000));
            runtime.sleep(2000);
            console.hide();

            click(597, 475);
            runtime.sleep(1000);
            click(597, 475);
        }

        if (dealFlashWin(5)){
            return false
        }
        if (text("已请求").findOne(100)){
            return false
        }
    } else {
        toastLog(TAG + "关注失败");
        return false
    };

    toastLog(TAG + "开始发送私信消息");
    // 点击私信
    try {
        id("cst").findOne(3000).click();
    } catch (e) {
        click(850, 472);
        click(850, 472);
    }

    dealFlashWin(1);
    runtime.sleep(500);
    ready_send_msgs = SAVED.get("dy_sx_zh", "").split("\n");
    if (ready_send_msgs.length < 1) {
        ErrorHandle("招呼设置错误", true);
    }

    for (let i = 0; i < ready_send_msgs.length; i++) {
        a = className("android.widget.EditText").findOne(1000);
        a.click();
        let t = ready_send_msgs[i];
        console.verbose(TAG + "设置消息:[" + i + "] " + t);
        className("android.widget.EditText").findOne(2000).setText(t);
        runtime.sleep(1000);
        className("android.widget.EditText").findOne(2000).setText(t);
        className("android.widget.EditText").findOne(2000).setText(t);
        runtime.sleep(500);
        desc("发送").findOne(6000).click();
        // runtime.sleep(i * 1500);
        runtime.sleep((3 - i) * 2000);
    }
    COUNT++;
    toastLog(TAG + "三条消息发送完毕");
    toastLog(TAG + "当前执行成功数 " + COUNT);
}


function returnListPage() {
    while (!textEndsWith("条评论").findOne(2000)) {
        if (id("j4").findOne(2000)) {
            clickHelper(id("j4").findOne(), false, false, true);
        } else {
            desc("返回").findOne(2000).click();
        }
        toastLog("点击返回");
    }
    return true
}

function Start() {

    // 判断是否在评论界面
    toastLog(TAG + "等待打开抖音界面");
    runtime.sleep(3000);
    let tc = 0;
    while (!textEndsWith("条评论").findOne(1500)) {
        tc++;
        if (tc > 30) {
            toastLog(TAG + "三秒内未发现抖音界面,脚本退出");
            ErrorHandle("未发现评论页面,脚本退出", true);
        }
    }
    do {
        // 当前页面处理逻辑
        // try {
        returnListPage();
        runtime.sleep(5000);
        CARD_LIST = className("android.support.v7.widget.RecyclerView").findOne().children();
        SIZE = CARD_LIST.size();
        toastLog(TAG + "发现当前页面存在评论人数: " + SIZE);
        CARD_LIST.each(
            function (CARD_UI) {
                // 解开包装
                returnListPage();
                CARD_UI_INNER = CARD_UI.children().get(0);
                // 点进个人页面
                console.hide();
                runtime.sleep(2000);
                if (!CARD_UI_INNER) {
                    // console.log(CARD_UI_INNER.size());
                    // console.log(CARD_UI_INNER);
                    console.error(currentActivity());
                    console.error(TAG + "元素定位错误!");
                    return
                }

                // 不要去点击回复
                if (CARD_UI_INNER.children().size() <= 3) {
                    return
                }
                clickHelper(CARD_UI_INNER.children().get(0), false, false, true);
                runtime.sleep(2000);
                toastLog(TAG + "即将进入消息发送函数");
                SendMsg();
                return

            }
        )
        console.log(TAG + "当前页面扫描完毕,自动下拉一页");
        // } catch (e) {
        // console.error(e.message + "\n" + e.stack);
        // ErrorHandle(e.message + "\n\n" + e.stack, false);
        // continue
        // }
    } while (returnListPage() && className("android.support.v7.widget.RecyclerView").findOne().scrollForward());
    console.warn(TAG + "当前评论页面扫描发送完毕,请检查!")
}

# 处理突然闪出来的窗口
function dealFlashWin(WIN_TYPE) {
    switch (WIN_TYPE) {
        case 1:
            ui = text("确认").findOne(500);
            if (ui){
                ui.click();
                return true
            }
        case 2:
            ui = text("我知道了").findOne(500);
            if (ui){
                ui.click();
                return true
            }
        case 3:
            ui = text("好的").findOne(500);
            if (ui){
                ui.click();
                return true
            }
        case 4:
            ui = text("以后再说").findOne(500);
            if (ui){
                ui.click();
                return true
            }
        case 5:
            ui = text("取消").findOne(500);
            if (ui){
                ui.click();
                return true
            }
        default:
            return false
    }
    
}

// 172.16.6.114
// console.show();
// console.info("调试-》设备像素:", device.width, device.height);
// console.info("注意-》必须给予截图权限并记住");
// console.setSize(device.width / 2, device.height / 2);
// console.setPosition(0, device.height / 2);


// work_thread = threads.start(function () {
//     Start();
// });



threads.start(function(){
    setInterval(function(){
        console.verbose("监控弹窗ing.....");
        dealFlashWin(2);
        dealFlashWin(3);
        dealFlashWin(4);
    },500);
    
})

Start();

// SendMsg();
// returnListPage();
// className("android.support.v7.widget.RecyclerView").findOne().scrollForward();
// SAVED.put("dy_sx_zh","咦\n好像在哪里见过你来着,眼熟\n我想想。。。");
// console.info(device.width,device.height);
// engines.stopAll();

enter.js(界面或入口代码)

"ui";

ui.statusBarColor("#036289");
ui.layout(
    <drawer id="drawer">
        <vertical>
            <appbar>
                <toolbar id="toolbar" title="抖音助手 V1.0.0" />
                <tabs id="tabs" />
            </appbar>

            <viewpager id="viewpager">
                <frame>
                    <ScrollView>
                        <vertical margin="8 8 8 8">
                            <horizontal marginTop="10"><View margin="10 0 12 0" w="5" h="20" bg="#F75000"></View><text textSize="15" textColor="#F75000">软件权限</text></horizontal>
                            <Switch id="autoService" text="自动操作辅助权限" checked="{{auto.service != null}}"  textSize="15sp" />
                            <Switch id="overlayService" text="迷你窗口显示权限" checked="{{auto.service != null}}" textSize="15sp" />
                            <horizontal marginTop="10"><View margin="10 0 12 0" w="5" h="20" bg="#F75000"></View><text textSize="15" textColor="#F75000">养号操作</text></horizontal>
                            <horizontal layout="center" >
                                <button id="dy_yh_1" text="首页|搜索" w="auto" style="Widget.AppCompat.Button.Colored" />
                                <button id="dy_yh_2" text="直播" w="auto" style="Widget.AppCompat.Button.Colored" />
                                <button id="exit" text="退出" w="*" style="Widget.AppCompat.Button.Colored" />
                            </horizontal>
                            <horizontal marginTop="10"><View margin="10 0 12 0" w="5" h="20" bg="#F75000"></View><text textSize="15" textColor="#F75000">私信操作</text></horizontal>
                            <text text="下面自定义3条招呼(换行分隔)" textColor="#036289" padding="8 8 8 8" />
                            <input id="dy_sx_zh" lines="3" maxLines="3" textSize="12sp" textColor="#D87093" textStyle="italic" padding="8 8 8 8" />
                            <button id="dy_sx_1" text="在评论页根据性别进行私信" w="auto" style="Widget.AppCompat.Button.Colored" />
                        </vertical>
                    </ScrollView>
                </frame>

                <frame>
                    <ScrollView>
                        <vertical>
                            <text id="desc" line="40" maxLines="100" textColor="#228B22" gtavity="left" padding="8 8 8 8"></text>
                        </vertical>
                    </ScrollView>

                </frame>

                <frame>
                <ScrollView>
                    <vertical layout="center">
                        <vertical padding="8">                        
                        <text text="抖音号批量操作输入" textColor="#036289" padding="8 8 8 8" />
                        <input id="dy_dyhs" lines="6" maxLines="8" textSize="12sp" textColor="#036289" textStyle="italic" />
                        <button id="script_save_set" text="保存设置" w="auto" />

                            <horizontal layout="">
                                <text textSize="16sp" textColor="black" text="服务器电脑地址:" />
                                <input id="ip" hint="请看后台输出的地址" />
                                <button id="set_ip" text="设置" w="*" />
                            </horizontal>

                            <horizontal marginTop="10"><View margin="10 0 12 0" w="5" h="20" bg="#D87093"></View><text textSize="15" textColor="#D87093">开发专用</text></horizontal>
                            <button id="ywjb" text="杂项" w="*" style="Widget.AppCompat.Button.Colored" />
                        </vertical>
                    </vertical>
                   </ScrollView>
                </frame>
            </viewpager>
        </vertical>

        <vertical layout_gravity="left" bg="#ffffff" w="230">
            <list id="menu">
                <horizontal>
                    <text textColor="#036289" textSize="27sp" text="{{this.title}}" h="40" layout_gravity="center" marginBottom="10" />
                </horizontal>
            </list>
        </vertical>
    </drawer>
);
var saved = storages.create("dyzs");
activity.setSupportActionBar(ui.toolbar);
ui.viewpager.setTitles(["脚本控制", "公告", "设置"]);
ui.tabs.setupWithViewPager(ui.viewpager);
ui.toolbar.setupWithDrawer(ui.drawer);
ui.autoService.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时,跳转到页面让用户去开启
    if (checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});

ui.overlayService.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时,跳转到页面让用户去开启
    if (checked) {
        int = app.startActivity({
            packageName: "com.android.settings",
            className: "com.android.settings.Settings$AppDrawOverlaySettingsActivity",
            data: "package:" + auto.service.getPackageName().toString()
        });
        toast("请打开悬浮窗开关");
    }

    // if (!checked && auto.service != null) {
    //     auto.service.disableSelf();
    //     toast("已关闭迷你窗口显示权限");
    // }
});


// 菜单
ui.menu.setDataSource([
    {
        title: "关于软件"
    },
    {
        title: "软件注册"
    }
])

// 菜单
ui.menu.on("item_click", item => {
    switch (item.title) {
        case "关于软件":
            toast("关于软件-功能未完善,敬请期待");
            break;
        case "软件注册":
            toast("软件注册-功能未完善,敬请期待");
    }
})

// 当用户回到本界面时,resume事件会被触发
ui.emitter.on("resume", function () {
    // 此时根据无障碍服务的开启情况,同步开关的状态
    ui.autoService.checked = auto.service != null;
});

// 业务-服务器IP
ui.ip.setText(saved.get("ydt_server_ip", "172.16.2.207"));
ui.set_ip.click(() => {
    saved.put("ydt_server_ip", ui.ip.text());
    toastLog("设置Ip地址成功:" + ui.ip.text());
})

// ui.dy_dyhs.setText(saved.get("dy_dyhs",[]));
// ui.script_save_set.click(function(){
//     // 保存抖音号
//     saved.put("dy_dyhs",ui.dy_dyhs.text());
//     console.show();
//     console.info("批量处理抖音号设置如下:");
//     console.verbose(saved.get("dy_dyhs",[]));
//     setTimeout(()=>{
//         console.hide();
//     },5000)
// })


ui.ywjb.on("click", function () {
    // 启动业务脚本
    engines.execScriptFile("业务脚本1.6.js")
});


// ui.start.on("click", function () {
//     //程序开始运行之前判断无障碍服务
//     if (auto.service == null) {
//         toast("[伊的推Andoird工具]请先开启无障碍服务!");
//         return;
//     }
// });

ui.desc.setText("运行条件:安卓7.0+ 、最新抖音版本\n\n")

ui.exit.click(() => {
    toast("成功退出");
    engines.stopAll();
})

ui.dy_yh_1.click(() => {
    saved.put("dy_mode", "yh_1");
    engines.execScriptFile("抖音.js");
})

ui.dy_yh_2.click(() => {
    saved.put("dy_mode", "yh_2");
    engines.execScriptFile("抖音.js");
})

sx_content = saved.get("dy_sx_zh","");
if(sx_content == ""){
    sx_content = "hi\n你视频拍得不错啊\n交个朋友呗"
}
ui.dy_sx_zh.setText(sx_content);
ui.dy_sx_1.click(()=>{
    zh_content = ui.dy_sx_zh.text();
    saved.put("dy_sx_zh", zh_content);
    toastLog("已设置3条招呼:\n" + zh_content);
    engines.execScriptFile("私信.js");
})

个人公众号,有兴趣请关注,往后会很多一些相关文章。
Auto.Js 抖音脚本 性别识别 消息自定义 免ROOT_第1张图片

你可能感兴趣的:(安卓自动化)