/**

  • Hamibot 脚本:OPPO商店自动浏览任务
  • 适配平台:Hamibot
  • 功能:自动清理后台,依次打开软件商店和主题商店,完成日常浏览任务。
    */

// ========== 1. 环境检查与全局初始化 ==========
const device = hamibot.device;

/**

  • 检查并请求脚本运行所需权限

  • @returns {boolean} 所有权限是否已就绪
    */
    function checkPermissions() {
    const requiredPermissions = ['accessibility', 'floaty', 'storage'];
    let missingPermissions = [];

    requiredPermissions.forEach(perm => {
    if (!hamibot.hasPermission(perm)) {
    missingPermissions.push(perm);
    }
    });

    if (missingPermissions.length > 0) {
    console.warn(缺少权限: ${missingPermissions.join(', ')},正在请求...);
    hamibot.requestPermissions(missingPermissions);
    hamibot.sleep(2000); // 等待用户操作
    return false;
    }
    return true;
    }

/**

  • 等待无障碍服务开启
    */
    function waitForAccessibility() {
    if (!hamibot.accessibilityEnabled()) {
    console.log("无障碍服务未开启,正在请求...");
    hamibot.requestAccessibility();
    hamibot.sleep(3000); // 等待用户授权
    }
    }

// 检查截图权限
function checkScreenCapturePermission() {
if (!requestScreenCapture()) {
console.error("请求截图权限失败,请在Hamibot设置中手动开启。");
return false;
}
return true;
}

// ========== 2. 通用工具函数 ==========
/**

  • 拟人化点击:在控件范围内随机取坐标点击
  • @param {UiObject} uiObject - 控件对象
    */
    function smartClick(uiObject) {
    if (!uiObject) return;
    let bounds = uiObject.bounds();
    let x = random(bounds.left + 10, bounds.right - 10);
    let y = random(bounds.top + 10, bounds.bottom - 10);
    hamibot.click(x, y);
    hamibot.sleep(random(300, 800));
    }

/**

  • 拟人化滑动:执行一次随机的屏幕滑动
    */
    function humanSwipe() {
    let w = device.width, h = device.height;
    let startX = random(w * 0.2, w * 0.8);
    let startY = random(h * 0.3, h * 0.7);
    let endX = random(w * 0.2, w * 0.8);
    let endY = random(h * 0.3, h * 0.7);
    let duration = random(300, 800);
    hamibot.swipe(startX, startY, endX, endY, duration);
    hamibot.sleep(random(500, 1500));
    }

/**

  • 模拟真人停留并随机滑动
  • @param {number} minTime - 最小停留时间(秒)
  • @param {number} maxTime - 最大停留时间(秒)
    */
    function simulateHumanStay(minTime = 20, maxTime = 60) {
    let totalTime = random(minTime * 1000, maxTime * 1000);
    let startTime = new Date().getTime();
    console.log(开始模拟真人浏览,预计停留 ${totalTime/1000} 秒...);
    while (new Date().getTime() - startTime < totalTime) {
    humanSwipe();
    hamibot.sleep(random(2000, 5000));
    }
    }

/**

  • 清理后台所有应用
    */
    function clearAllBackgroundApps() {
    console.log("正在清理后台应用...");
    hamibot.home();
    hamibot.sleep(1000);
    hamibot.recents();
    hamibot.sleep(1000);
    let clearBtn = hamibot.text("清除").findOne(2000) ||
    hamibot.text("全部清理").findOne(2000) ||
    hamibot.desc("清除").findOne(2000);
    if (clearBtn) {
    smartClick(clearBtn);
    hamibot.sleep(1000);
    } else {
    // 如果找不到清理按钮,则向上滑动
    hamibot.swipe(device.width / 2, device.height / 2, device.width / 2, device.height / 4, 300);
    }
    hamibot.home();
    hamibot.sleep(1000);
    }

// ========== 3. 核心任务函数 ==========
/**

  • 执行软件商店任务
    */
    function taskAppStore() {
    console.log("🚀 开始执行【软件商店】任务");
    hamibot.launchPackage("com.heytap.market");
    hamibot.sleep(3000);

    let myTab = hamibot.text("我的").findOne(5000);
    if (myTab) {
    smartClick(myTab);
    } else {
    console.log("未找到'我的'页面,跳过任务。");
    return;
    }
    hamibot.sleep(2000);

    let rewardBtn = hamibot.text("领取奖励").findOne(5000);
    if (rewardBtn) {
    smartClick(rewardBtn);
    hamibot.sleep(2000);
    }

    while (true) {
    let openBtn = hamibot.text("打开").findOne(3000);
    if (openBtn) {
    smartClick(openBtn);
    simulateHumanStay(10, 30); // 模拟浏览
    hamibot.back();
    hamibot.sleep(2000);
    } else {
    console.log("✅ 【软件商店】任务已完成");
    break;
    }
    }
    }

/**

  • 执行主题商店任务
    */
    function taskThemeStore() {
    console.log("🚀 开始执行【主题商店】任务");
    hamibot.launchPackage("com.heytap.themestore");
    hamibot.sleep(3000);

    let recommend = hamibot.text("推荐").findOne(5000);
    if (recommend) {
    smartClick(recommend);
    hamibot.sleep(2000);
    } else {
    console.log("未找到'推荐'页面,跳过任务。");
    return;
    }

    let freeBtn = hamibot.text("免费领取").findOne(5000);
    if (freeBtn) {
    smartClick(freeBtn);
    hamibot.sleep(2000);
    }

    while (true) {
    let browseBtn = hamibot.text("去浏览").findOne(3000);
    if (browseBtn) {
    smartClick(browseBtn);
    simulateHumanStay(10, 30); // 模拟浏览
    hamibot.back();
    hamibot.sleep(2000);
    } else {
    console.log("✅ 【主题商店】任务已完成");
    break;
    }
    }
    }

// ========== 4. 主程序入口 ==========
function main() {
console.log("📝 脚本初始化开始...");
if (!checkPermissions()) {
console.error("权限检查未通过,脚本退出。");
return;
}
waitForAccessibility();
if (!checkScreenCapturePermission()) {

HAMI_7437302066 · 3 小时前

回答

Hamibot AI
1
· 3 小时前

你的脚本运行后很快提示“运行结束”,常见原因有两个:权限未授予或脚本主流程逻辑不完整。

1. 权限问题

大多数自动化脚本依赖无障碍服务和后台弹出权限,如果未授予,脚本会直接退出。请检查:

  • 进入手机「设置」→「无障碍」→ 开启 Hamibot 的无障碍服务。
  • 进入手机「设置」→「应用管理」→ 找到 Hamibot → 授予“后台弹出界面”权限。
  • 如果脚本使用了截图功能,还需要在 Hamibot 中开启截图权限。

2. 脚本逻辑不完整

你的脚本 main() 函数中,权限检查通过后没有调用任何任务函数(如 taskAppStore()taskThemeStore()),导致脚本直接运行结束。应将主程序修改为:

function main() {
    console.log("📝 脚本初始化开始...");
    if (!checkPermissions()) {
        console.error("权限检查未通过,脚本退出。");
        return;
    }
    waitForAccessibility();
    // 若不需要截图权限可删除下面两行
    if (!checkScreenCapturePermission()) {
        return;
    }
    clearAllBackgroundApps();
    taskAppStore();
    taskThemeStore();
    console.log("🎉 所有任务执行完毕");
}
main();

另外,脚本中 requestScreenCapture() 需要在 Hamibot 中开启截图权限,且该函数只能在脚本开头调用一次,如果不需要截图功能可以移除。

3. 兼容性建议

脚本开头添加文档注释,方便查阅:

// 文档地址 https://docs.hamibot.com/

如果问题仍然存在,请检查手机型号是否兼容(OPPO 系需 ColorOS 11+),并确保 Hamibot 是最新版本。

ℹ️ 内容由 AI 生成,可能不准确,请注意核实