脚本例子

为了方便开发者尽快熟悉脚本,提供如下脚本仅供参考:

1.hello world!(进入微信,并滚动三次)
function main() {
    if (1 == launchApp('com.tencent.mm', 'txt:发现|txt:搜索')) {
        toast('进入微信成功');
        var index = 0;
        while (index < 3) {
            scroll('up', {distance: Math.random() * 0.5 + 0.45, duration: parseInt(Math.random() * 200 + 300), afterWait: 3000});
            ++index;
            toast('滚动:' + index);
        }
        toast('任务结束,退出');
    } else {
        toast('启动微信失败');
    }
}
                                    
2.微信群自动抢红包
function main() {
    var __global redComing = false;
    console.log('等待红包来');
    while (true) {
        if (redComing) {
            console.log('红包来了');
            var ret = findView('txt:微信红包', {flag: 'find_all', maxStep:10});
            while (ret.length > 0) {
                var find = false;
                for (var item of ret.views) {
                    if (item.parent.parent[0][1].size==1 && item.parent.parent[0][1][0].text != '已领取') {
                        click(item, {click:true});
                        ret = switchPage('txt:开', 'txt^:已存入零钱|txt^:手慢了');
                        if (ret.length > 0) {
                            if (ret.views[0].text.includes('已存入零钱')) {
                                console.log('领取成功了');
                            } else {
                                console.log('手慢了,没有领取到');
                            }
                            find = true;
                            back2Page('txt^:群聊');
                        } else {
                            back2Page('txt^:群聊', {backFirst: false});
                        }
                        break;
                    }
                }

                if (!find) {
                    break;
                }
                ret = findView('txt:微信红包', {flag: 'find_all', maxStep:10});
            }

            redComing = false;
            refresh();
        } else {
            sleep(500);
        }
    }
}

function cbNotification(textList, className, packageName, rawEvent) {
    for (var item of textList) {
        if (item && item.includes('微信红包')) {
            openNotification(rawEvent);
            redComing = true;
            break;
        }
    }
}
                                    
3.自动运行APP,该脚本在小米手机中测试通过,其他品牌手机有可能ui会有所不同
// app不存在则安装,版本低则更新,最后再启动app
function main(packageName, autoInstall=true, checkUpdate=true, afterWait=6000) {
    var result = false, version = getAppVersion(packageName), appName = getAppName(packageName);
    if (appName == '') {
        console.log('没有安装app');
        if (autoInstall) {
            if (installApp(packageName)) {
                result = 1 == launchApp(packageName);
            }
        }
    } else {
        if (checkUpdate) {
            var latestVersion = getLatestAppVersion(packageName);
            if (latestVersion != '') {
                var version = getAppVersion(packageName);
                if (compareVersion(latestVersion, version)) {
                    // update
                    if (!installApp(packageName)) {
                        console.log('安装失败');
                    }
                }
            }
        }

        result = 1 == launchApp(packageName);
    }

    if (result) {
        clickByTag('txt:允许', {click: true, maxStep: 8});
        sleep(afterWait);
    }

    return result;
}

function compareVersion(newVersion, oldVersion) {
    if (newVersion && oldVersion) {
        newVersion = newVersion.trim();
        oldVersion = newVersion.trim();
        var newArr = newVersion.split('.'), oldArr = oldVersion.split('.');
        var size = Math.min(newArr.length, oldArr.length);
        for (var i = 0; i < size; ++i) {
            var newV = parseInt(newArr[i]);
            var oldV = parseInt(oldArr[i]);
            if (newV > oldV) {
                return true;
            } else if (newV < oldV) {
                break;
            }
        }
    }

    return false;
}

function installApp(packageName) {
    var savedPath = downloadApp(packageName);
    if (savedPath != '') {
        if (install(savedPath)) {
            // 开始点击界面
            console.log('开始点击安装界面');
            sleep(2000);
            var findInstall = false, clickInstall = false, startTime = new Date().getTime();
            while (!clickInstall && (new Date().getTime() - startTime) < 100000) {
                var ret = findView('txt*:解析软件包时出现问题|id:com.miui.packageinstaller:id/install_btn|txt:继续安装|txt*:无视风险安装|txt:退出登录');
                if (ret.length > 0) {
                    if (ret.tag == 'com.miui.packageinstaller:id/install_btn' || ret.tag == '继续安装') {
                        click(ret.views[0], {click: true});
                    } else if (ret.tag == '退出登录') {
                        break;
                    } else if (ret.tag == '无视风险安装') {
                        var view = ret.views[0];
                        gestureClick(view.right - 100, (view.top + view.bottom) / 2, {afterWait:1000});
                    } else {
                        console.log('解析软件包失败');
                        clickByTag('txt:确定', {click: true});
                        break;
                    }
                    findInstall = true;
                }

                if (findInstall) {
                    ret = findView('txt:确定|id:com.miui.packageinstaller:id/done_button');
                    if (ret.length > 0) {
                        click(ret.views[0], {click: true});
                        clickInstall = true;
                    }
                }
            }

            console.log('完成安装:' + clickInstall);
            return clickInstall;
        } else {
            console.log('install failed:');
        }
    }

    return false;
}
                                    
4.自动设置APP权限,该脚本在小米手机中测试通过,其他品牌手机有可能ui会有所不同
// 禁止app「获取手机中已安装的应用信息」权限
function main(appName) {
    if (!launchApp('com.android.settings')) {
        return false;
    }

    var ret = smartFindView('txt:设置', {failed: exitPopup, maxStep: 30});
    if (ret.length <= 0) {
        console.error('启动设置程序失败');
        return false;
    }

    scroll('up', {distance: 0.99, afterWait: 1000});

    var result = false, scrollCount = 0, target = 'txt:应用设置|txt:授权管理|';
    while(scrollCount <= 3 && target!= '') {
        ret = smartFindView(target, {failed: exitPopup});
        if (ret.length <= 0) {
            console.log('没找到tag,滚动后继续找');
            scrollCount++;
            scroll('up', {distance: 0.99, afterWait: 1000});
        } else {
            result = true;
            if (ret.tag == '应用设置') {
                result = switchPage('txt:应用设置', 'txt:授权管理', {failed: exitPopup, click: true}).length > 0;
            }

            if (result) {
                result = false;
                ret = switchPage('txt:授权管理', 'txt:应用权限管理', {failed: exitPopup, click: true});
                if (ret.length > 0) {
                    ret = switchPage('txt:应用权限管理', 'txt:应用管理', {failed: exitPopup, click: true});
                    if (ret.length > 0) {
                        if (clickByTag('txt$:个应用程序', {click: true})) {
                            ret = findView('cn:miui.widget.ClearableEditText');
                            if (ret.length > 0) {
                                paste(ret.views[0], appName, {type: 'paste', afterWait: 3000});

                                var container = findView('cn:androidx.recyclerview.widget.RecyclerView');
                                if (container.length > 0 && container.views[0].size > 0) {
                                    click(container.views[0][0], {click: true, afterWait: 1000});
                                    ret = findView('txt:读取应用列表', {maxStep: 6});
                                    if (ret.length > 0) {
                                        if (ret.views[0].parent.parent[1].text != '拒绝') {
                                            ret = switchPage('txt:获取手机中已安装的应用信息', 'txt:取消', {click: true, failed: exitPopup});
                                            if (ret.length > 0) {
                                                result = clickByTag('txt:拒绝', {click: true, afterWait: 2000});
                                            } else {
                                                console.error('失败');
                                            }
                                        } else {
                                            console.error('已经是拒绝了');
                                            result = true;
                                        }
                                    }
                                } else {
                                    console.error('can not find recyclerView');
                                }
                            } else {
                                console.error('can not find edit');
                            }
                        }
                    } else {
                        console.error('没进入应用管理');
                    }
                }
            }
            break;
        }
    }

    return result;
}

function exitPopup() {
    var ret = findView('txt:暂不开启', {maxStep: 1});
    if (ret.length > 0) {
        click(ret.views[0], {click: true});
    }
}

                                    
5.自动删除没有使用的APP,该脚本在小米手机中测试通过,其他品牌手机有可能ui会有所不同
// 删除长时间没有使用的app,系统app和白名单中的app不删除
function main(whiteList=[]) {
    if (!launchApp('com.miui.securitycenter')) {
        return;
    }

    var ret = smartFindView('txt:安全中心', {maxStep: 30});
    if (ret.length <= 0) {
        console.log('启动安全中心失败');
        return;
    }

    whiteList.push('微信');
    whiteList.push('支付宝');

    ret = switchPage('txt:应用管理', 'txt:应用卸载', {click: true});
    if (ret.length > 0) {
        ret = switchPage('txt:应用卸载', 'txt^:卸载选中应用|txt^:一键卸载', {click: true});
        if (ret.length > 0) {
            ret = findView('txt:存储占用|txt:使用频率|txt:安装时间');
            if (ret.length > 0) {
                if (ret.tag != '使用频率') {
                    click(ret.views[0], {click: true, afterWait: 1000});
                    ret = findView('txt:使用频率');
                    if (ret.length > 0) {
                        click(ret.views[0], {click: true, afterWait: 1000});
                    }
                }

                var selectedCount = 0, index = 0;
                var container = findView('cn:androidx.recyclerview.widget.RecyclerView');
                while (container.length > 0 && index < container.views[0].size) {
                    var item = container.views[0][index];
                    if (item[0].size == 4) {
                        if (item[0][1][1].text.includes('未使用')) {
                            if (!item[0][3][0].checked) {
                                if (whiteList.indexOf(item[0][1][0].text) < 0) {
                                    click(item, {click: true});
                                    selectedCount++;
                                }
                            }
                        } else {
                            break;
                        }
                    } else if (item[0].size == 3 && item[0][1].size == 3) {
                        console.log('item[0][1][2].text:' + item[0][1][2].text);
                        if (item[0][1][2].text.includes('未使用')) {
                            if (!item[0][2].checked) {
                                console.log('txt:' + item[0][1][0].text);
                                if (whiteList.indexOf(item[0][1][0].text) < 0) {
                                    click(item[0], {click:true});
                                    selectedCount++;
                                }
                            }
                        } else {
                            break;
                        }
                    } else {
                        console.log('忽略一个item');
                    }

                    ++index;
                    container = findView('cn:androidx.recyclerview.widget.RecyclerView');
                }

                if (selectedCount > 0) {
                    ret = switchPage('txt^:卸载选中应用|txt^:一键卸载', 'txt:确定|txt:卸载选中应用', {click: true});
                    if (ret.length > 0) {
                        click(ret.views[0], {click: true, afterWait: 3000});

                        var startTime = new Date().getTime();
                        ret = findView('txt:应用卸载');
                        while (ret.length <= 0 && (new Date().getTime() - startTime) < 120000) {
                            ret = findView('txt:应用卸载', {afterWait: 1000});
                        }
                        console.log('卸载成功');
                    }
                } else {
                    console.log('没有需要卸载的');
                }
            }
        }
    } else {
        console.error('进入优化加速失败');
    }
}

                                    
6.自动设置「冰狐智能辅助」的相关权限,该脚本在小米手机中测试通过,其他品牌手机有可能ui会有所不同
function main() {
    var result = 0;

    if (!launchApp('com.android.settings')) {
        console.error('启动失败');
        return result;
    }

    var ret = smartFindView('txt:设置', {failed: exitPopup, maxStep:50});
    if (ret.length <= 0) {
        console.error('找不到容器');
        return result;
    }

    var target = 'txt:我的设备|txt:双卡和移动网络|txt:锁屏|txt:电池与性能|txt:应用设置|txt:授权管理|', scrollCount = 0, successCount = 0;
    while (scrollCount <= 3 && target != '') {
        ret = smartFindView(target, {failed: exitPopup});
        if (ret.length <= 0) {
            scrollCount++;
            console.log('scroll');
            scroll('up', {distance: 0.99, afterWait: 1000});
        } else {
            var text = ret.views[0].text;
            console.log('text:' + text);

            if (text == '我的设备') {
                if (update()) {
                    successCount++;
                } else {
                    result = -4;
                    console.error('我的设备设置失败');
                }
            } else if (text == '双卡和移动网络') {
                if (network()) {
                    successCount++;
                } else {
                    result = -8;
                    console.error('移动网络设置失败');
                }
            } else if (text == '锁屏') {
                if (lockScreen()) {
                    successCount++;
                } else {
                    result = -5;
                    console.error('锁屏设置失败');
                }
            } else if (text == '电池与性能') {
                if (performance()) {
                    successCount++;
                } else {
                    result = -6;
                    console.error('电池与性能失败');
                }
            } else if (text == '应用设置') {
                target = target.replace('授权管理|', '');
                result = app();
                if (result == 1) {
                    successCount++;
                }
            } else if (text == '授权管理') {
                target = target.replace('应用设置|', '');
                result = app(false);
            }

            target = target.replace(text+'|', '');
        }
    }

    if (successCount == 4) {
        result = 1;
    }

    console.log('end:' + result);

    exitAppDeal();

    return result;
}

function app(doFirst=true) {
    var result = -7, success = true;
    if (doFirst) {
        var ret = switchPage('txt:应用设置', 'txt:授权管理', {click: true, failed: exitPopup});
        success = ret.length > 0;
    }

    if (success) {
        var ret = switchPage('txt:授权管理', 'txt:应用权限管理', {click: true, failed: exitPopup});
        if (ret.length > 0) {
            ret = switchPage('txt:应用权限管理', 'txt:应用管理', {click: true, failed: exitPopup});
            if (ret.length > 0) {
                ret = findView('txt$:个应用程序');
                if (ret.length > 0) {
                    click(ret.views[0], {click: true});
                    ret = findView('cn:miui.widget.ClearableEditText');
                    if (ret.length > 0) {
                        console.log('do paste:' + getAppName());
                        paste(ret.views[0], getAppName(), {type: 'paste', afterWait: 4000});

                        var container = findView('cn:androidx.recyclerview.widget.RecyclerView');
                        if (container.length > 0 && container.views[0].size > 0) {
                            console.log('click sub item');
                            click(container.views[0][0], {click: true, afterWait: 1000});
                            scroll('up', {distance: 0.99, afterWait: 1000});

                            if (setAppPermission('允许应用在锁屏上显示')) {
                                if (setAppPermission('允许应用在后台弹出界面')) {
                                    result = 1;
                                } else {
                                    console.log('设置 允许应用在后台弹出界面失败');
                                }
                            } else {
                                console.log('设置 锁屏上显示应用失败');
                            }
                        } else {
                            console.log('can not find recyclerView:' + container.views[0].size);
                        }
                    } else {
                        console.log('can not find edit');
                    }
                }

                if (result == 1) {
                    result = -2;
                    if (back2Page('txt:授权管理', {failed: exitPopup})) {
                        ret = switchPage('txt:自启动管理', 'txt:更多', {click: true, failed: exitPopup});
                        if (ret.length > 0) {
                            console.log('start find');
                            var container = smartFindView('cn:androidx.recyclerview.widget.RecyclerView', {failed: exitPopup});
                            var count = 0, appName = getAppName();
                            while (container.views[0].size > 0 && count < 20) {
                                var find = false;
                                for (var item of container.views[0]) {
                                    if (item[1].className=='android.widget.TextView' && item[1].text==appName) {
                                        if (!item[2].checked) {
                                            click(item[2], {click:true});
                                        }
                                        find = true;
                                        break;
                                    } else if (item[1][0][0].className=='android.widget.TextView' && item[1][0][0].text==appName) {
                                        if (!item[2].checked) {
                                            click(item[2], {click: true});
                                        }
                                        find = true;
                                        break;
                                    }
                                }

                                if (find) {
                                    console.log('ok----');
                                    result = 1;
                                    break;
                                }

                                console.log('scroll--');
                                count ++;
                                scroll('up', {root: container.views[0], type: 2, afterWait:1000});

                                container = smartFindView('cn:androidx.recyclerview.widget.RecyclerView', {failed: exitPopup});
                            }
                        } else {
                            console.error('进入自启动失败');
                        }
                    } else {
                        console.log('没有进入授权管理');
                    }
                }
            } else {
                console.log('没进入  应用管理');
            }
        }
    }

    return result;
}

function setAppPermission(itemName) {
    var result = false;
    var ret = findView('txt:' + itemName, {maxStep: 6});
    if (ret.length > 0) {
        console.log('找到 锁屏显示');
        if (ret.views[0].parent.parent[1].text == '拒绝') {
            ret = switchPage('txt:' + itemName, 'txt:取消', {click: true, failed: exitPopup});
            if (ret.length > 0) {
                ret = findView('txt:始终允许|txt:允许');
                if (ret.length > 0) {
                    click(ret.views[0], {click: true, afterWait: 2000});
                    result = true;
                }
            }
        } else {
            result = true;
            console.log('已经不是拒绝,所以不再');
        }
    } else {
        console.error('无法找到:' + itemName);
    }

    return result;
}

function performance() {
    var result = false;

    var ret = switchPage('txt:电池与性能', 'txt:省电模式', {click: true, failed: exitPopup});
    if (ret.length > 0) {
        ret = switchPage('txt:设置', 'txt:锁屏后断开数据', {click: true, failed: exitPopup});
        if (ret.length > 0) {
            if (ret.views[0].parent.parent[1][0][0][0].text!='从不') {
                ret = switchPage('txt:锁屏后断开数据', 'txt:30分钟后', {click: true, failed: exitPopup});
                if (ret.length > 0) {
                    click(ret.views[0].parent.parent.parent.parent.parent[3], {click: true});
                    result = true;
                }
            } else {
                result = true;
            }
        } else {
            console.log('找不到 锁屏后断开数据');
        }

        back2Page('txt:隐私保护|txt:锁屏', {failed: exitPopup});
    } else {
        console.log('进入性能失败');
    }

    return result;
}

function update() {
    var result = false;
    var ret = switchPage('txt:我的设备', 'txt:MIUI 版本', {click: true, failed: exitPopup});
    if (ret.length > 0) {
        ret = switchPage('txt:MIUI 版本', 'txt:菜单|txt:发现新版本', {click: true, failed: exitPopup});
        if (ret.length > 0) {
            if (ret.tag == '菜单') {
                ret = switchPage('txt:菜单', 'txt:系统更新设置', {click: true, failed: exitPopup});
                if (ret.length > 0) {
                    ret = switchPage('txt:系统更新设置', 'txt:自动下载', {click: true, failed: exitPopup});
                    if (ret.length > 0) {
                        if (ret.views[0].parent.parent[1][0].checked) {
                            click(ret.views[0], {click: true, afterWait: 1000});
                        }

                        ret = findView('txt:智能更新');
                        if (ret.length > 0 && ret.views[0].parent.parent[1][0].checked) {
                            click(ret.views[0], {click: true, afterWait: 1000});
                        }

                        ret = findView('txt:自动更新');
                        if (ret.length > 0 && ret.views[0].parent.parent[1][0].checked) {
                            click(ret.views[0], {click: true, afterWait: 1000});
                        }

                        ret = findView('txt:加入优先升级计划');
                        if (ret.length > 0 && ret.views[0].parent.parent[1][0].checked) {
                            click(ret.views[0], {click: true, afterWait: 1000});
                        }

                        result = true;
                    } else {
                        console.log('找不到自动下载');
                    }
                } else {
                    console.log('找不到系统更新设置');
                }
            } else {
                console.log('直接找到 发现新版本');
                result = true;
            }
        } else {
            console.log('进入设置失败');
        }

        back2Page('txt:蓝牙', {failed: exitPopup});
    } else {
        console.log('进入我的失败');
    }

    return result;
}

function network() {
    var result = false;

    var ret = switchPage('txt:双卡和移动网络', 'txt:启用数据网络', {click: true, failed: exitPopup});
    if (ret.length > 0) {
        if (ret.views[0].parent.parent[1][0].checked) {
            click(ret.views[0], {click: true, afterWait: 1000});

            ret = findView('txt:始终接收彩信');
            if (ret.length > 0 && ret.views[0].parent.parent[1][0].checked) {
                click(ret.views[0], {click: true, afterWait: 1000});
            }

            result = true;
        }

        back2Page('txt:蓝牙', {failed: exitPopup});
    }

    return result;

}

function lockScreen() {
    var result = false;
    var ret = switchPage('txt:锁屏', 'txt:自动锁屏', {click: true, failed: exitPopup});
    if (ret.length > 0) {
        if (ret.views[0].parent.parent[1][0][0].text!='屏幕永不休眠') {
            ret = switchPage('txt:自动锁屏', 'txt:永不', {click: true, failed: exitPopup});
            if (ret.length > 0) {
                click(ret.views[0], {click: true});
                result = true;
            }
        } else {
            result = true;
        }
        back2Page('txt:蓝牙', {failed: exitPopup});
    }
    return result;
}

function exitPopup() {
    var ret = findView('txt:暂不开启|txt:关于自启动的说明|txt:进入首页', {maxStep:1});
    if (ret.length > 0) {
        console.log('text:' + ret.tag);
        if (ret.views[0].text == '关于自启动的说明') {
            clickByTag('txt:确定', {click: true});
        } else {
            click(ret.views[0], {click: true, afterWait: 1000});
        }
        return true;
    }
    return false;
}
                                    
7.自动设置锁定「冰狐智能辅助」,防止其被清理掉,该脚本在小米手机中测试通过,其他品牌手机有可能ui会有所不同
import sys_xiaomi_runtime;

function main() {
    var result = -1;

    if (!launchApp('com.miui.securitycenter')) {
        console.error('启动失败');
        return result;
    }

    var ret = waitAppLaunched('txt:安全中心', {failed: exitPopup, maxStep:30});
    if (ret.length <= 0) {
        console.error('找不到容器');
        return result;
    }

    ret = switchPage('txt:设置', 'txt:优化加速', {click: true, failed: exitPopup});
    if (ret.length > 0) {
        ret = switchPage('txt:优化加速', 'txt:锁定任务', {click: true, failed: exitPopup});
        if (ret.length > 0) {
            ret = switchPage('txt:锁定任务', 'txt:屏幕录制|txt:收音机', {click: true, failed: exitPopup});
            if (ret.length > 0) {
                var container = findView('cn:androidx.recyclerview.widget.RecyclerView', {maxStep:10});
                var count = 0, appName=getAppName();
                while (container.length > 0 && count < 20) {
                    var find = false;
                    for (var item of container.views[0]) {
                        if (item[1].className=='android.widget.TextView' && item[1].text==appName) {
                            if (!item[2].checked) {
                                click(item, {click: true, afterWait: 1000});
                            }
                            find = true;
                            break;
                        } else if (item[1][0].className=='android.widget.TextView' && item[1][0].text==appName) {
                            if (!item[2].checked) {
                                click(item, {click: true, afterWait: 1000});
                            }
                            find = true;
                            break;
                        }
                    }

                    if (find) {
                        result = 1;
                        break;
                    }

                    count++;
                    console.log('scroll');
                    scroll('up', {type: 2, root: container.views[0], afterWait: 1000});
                    container = findView('cn:androidx.recyclerview.widget.RecyclerView', {maxStep:10});
                }
            } else {
                console.error('进入锁定任务失败1');
            }
        } else {
            console.error('进入锁定任务失败');
        }
    } else {
        console.error('进入优化设置失败');
    }

    exitAppDeal();

    return result;
}


function exitPopup() {
    var ret = findView('txt*:用户须知', {maxStep:1});
    if (ret.length > 0 && ret.views[0].text.includes('用户须知')) {
        clickByTag('txt:同意', {click: true});
    } else {
        globalExitPopup();
    }
}