怎么添加脚本到悬浮窗方便运行

已存档
HAMI_6118405806 · 8 天前

回答

Hamibot AI
1
· 8 天前

要添加脚本到悬浮窗方便运行,可以通过以下两种主要方式实现:

方法一:创建带按钮的悬浮窗

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

// 创建悬浮窗
var floatyWin = floaty.window(
   <vertical padding="16" bg="#ffffff">
     <text text="脚本控制台" textSize="16sp" textColor="#000000" />
     <button id="script1Btn" text="运行脚本1" />
     <button id="script2Btn" text="运行脚本2" />
     <button id="script3Btn" text="运行脚本3" />
     <button id="exitBtn" text="关闭" />
   </vertical>
);

// 定义各个脚本的功能
function runScript1() {
    toast("开始运行脚本1");
    // 在这里添加脚本1的代码
    // 例如:auto.waitFor();
    // 你的脚本逻辑...
}

function runScript2() {
    toast("开始运行脚本2");
    // 在这里添加脚本2的代码
    // 你的脚本逻辑...
}

function runScript3() {
    toast("开始运行脚本3");
    // 在这里添加脚本3的代码
    // 你的脚本逻辑...
}

// 绑定按钮点击事件
floatyWin.script1Btn.click(function() {
    runScript1();
});

floatyWin.script2Btn.click(function() {
    runScript2();
});

floatyWin.script3Btn.click(function() {
    runScript3();
});

floatyWin.exitBtn.click(function() {
    floatyWin.close();
    hamibot.exit();
});

// 保持悬浮窗不被关闭
setInterval(function() {}, 1000);

方法二:动态加载外部脚本文件

如果你有多个独立的脚本文件,可以使用以下方式:

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

// 创建悬浮窗
var floatyWin = floaty.window(
   <vertical padding="16" bg="#ffffff">
     <text text="脚本管理器" textSize="16sp" textColor="#000000" />
     <button id="loadScript1" text="加载并运行脚本1" />
     <button id="loadScript2" text="加载并运行脚本2" />
     <button id="stopAll" text="停止所有脚本" />
     <button id="closeBtn" text="关闭管理器" />
   </vertical>
);

// 存储当前运行的脚本线程
var currentThread = null;

function runExternalScript(scriptPath) {
    // 停止之前运行的脚本
    if (currentThread && currentThread.isAlive()) {
        currentThread.interrupt();
    }
    
    // 加载并运行新脚本
    try {
        var scriptContent = files.read(scriptPath);
        currentThread = threads.start(function() {
            eval(scriptContent);
        });
        toast("脚本开始运行");
    } catch (e) {
        toast("加载脚本失败: " + e);
    }
}

// 绑定按钮事件
floatyWin.loadScript1.click(function() {
    runExternalScript("/sdcard/scripts/script1.js");
});

floatyWin.loadScript2.click(function() {
    runExternalScript("/sdcard/scripts/script2.js");
});

floatyWin.stopAll.click(function() {
    if (currentThread && currentThread.isAlive()) {
        currentThread.interrupt();
        toast("已停止当前脚本");
    }
});

floatyWin.closeBtn.click(function() {
    // 停止所有运行的脚本
    if (currentThread && currentThread.isAlive()) {
        currentThread.interrupt();
    }
    floatyWin.close();
    hamibot.exit();
});

// 保持悬浮窗不被关闭
setInterval(function() {}, 1000);

方法三:集成到现有脚本的悬浮窗控制面板

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

// 创建带状态显示的悬浮窗
var floatyWin = floaty.window(
   <vertical padding="16" bg="#ffffff" gravity="center">
     <text id="statusText" text="就绪" textSize="14sp" textColor="#333333" />
     <horizontal>
         <button id="startBtn" text="开始" w="60" />
         <button id="pauseBtn" text="暂停" w="60" />
         <button id="stopBtn" text="停止" w="60" />
     </horizontal>
     <button id="exitBtn" text="退出" />
   </vertical>
);

// 脚本状态管理
var scriptRunning = false;
var scriptPaused = false;

function updateStatus() {
    ui.run(function() {
        if (scriptRunning) {
            if (scriptPaused) {
                floatyWin.statusText.setText("已暂停");
            } else {
                floatyWin.statusText.setText("运行中");
            }
        } else {
            floatyWin.statusText.setText("就绪");
        }
    });
}

// 主脚本函数
function mainScript() {
    scriptRunning = true;
    updateStatus();
    
    // 你的脚本主循环
    while (scriptRunning) {
        if (!scriptPaused) {
            // 执行你的自动化任务
            toast("脚本运行中...");
            sleep(1000);
        } else {
            sleep(100);
        }
    }
}

// 绑定按钮事件
floatyWin.startBtn.click(function() {
    if (!scriptRunning) {
        threads.start(mainScript);
    }
    scriptPaused = false;
    updateStatus();
});

floatyWin.pauseBtn.click(function() {
    scriptPaused = !scriptPaused;
    updateStatus();
});

floatyWin.stopBtn.click(function() {
    scriptRunning = false;
    updateStatus();
});

floatyWin.exitBtn.click(function() {
    scriptRunning = false;
    floatyWin.close();
    hamibot.exit();
});

// 初始状态
updateStatus();

// 保持悬浮窗不被关闭
setInterval(function() {}, 1000);

最佳实践建议:

  1. 按钮设计:为每个常用脚本创建独立的按钮,方便一键启动
  2. 状态反馈:添加状态显示,让用户知道脚本当前运行状态
  3. 错误处理:添加try-catch块处理脚本加载和执行中的错误
  4. 线程管理:使用threads.start()来运行脚本,避免阻塞UI线程
  5. 资源清理:脚本退出时清理相关资源,避免内存泄漏

你可以根据实际需求选择合适的方法,第一种方法适合简单脚本,第二种方法适合管理多个独立脚本文件,第三种方法适合需要状态控制的复杂脚本。

ℹ️ 本回答由 AI 生成,内容仅供参考