如何在脚本里面添加开始和停止按钮

已存档
falymueji3 · 2 个月前

回答

Hamibot AI
1
· 2 个月前

可以使用 floaty.window 创建一个悬浮窗,并在其中添加“开始”和“停止”按钮。通过点击事件监听实现按钮的功能。

下面是一个示例代码,展示了如何实现开始和停止按钮:

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

var handlers = {
  running: false,

  start: function () {
    if (this.running) return;
    this.running = true;
    toast('任务开始');
    // 在这里编写你的开始逻辑
  },

  stop: function () {
    if (!this.running) return;
    this.running = false;
    toast('任务停止');
    // 在这里编写你的停止逻辑
  },
};

// 创建悬浮窗
var floatyWin = floaty.window(
  <vertical padding="16">
    <button id="startBtn" text="开始任务" bg="#4CAF50" />
    <button id="stopBtn" text="停止任务" bg="#F44336" enabled="false" />
  </vertical>
);

// 开始按钮点击事件
floatyWin.startBtn.click(function () {
  handlers.start();
  ui.run(function () {
    floatyWin.startBtn.enabled = false;
    floatyWin.stopBtn.enabled = true;
  });
});

// 停止按钮点击事件
floatyWin.stopBtn.click(function () {
  handlers.stop();
  ui.run(function () {
    floatyWin.startBtn.enabled = true;
    floatyWin.stopBtn.enabled = false;
  });
});

功能说明:

  1. 悬浮窗创建:使用 floaty.window 创建了一个包含两个按钮的垂直布局界面。
  2. 按钮功能
    • “开始任务”按钮:点击后执行 handlers.start(),并禁用该按钮,启用“停止任务”按钮。
    • “停止任务”按钮:点击后执行 handlers.stop(),并禁用该按钮,启用“开始任务”按钮。
  3. 任务逻辑:你可以在 handlers.start()handlers.stop() 中分别编写脚本开始和停止时需要执行的代码。

这样,你就可以通过点击悬浮窗中的按钮来控制脚本的运行状态了。

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