// 文档地址:https://docs.hamibot.com/
// 文档地址:https://docs.hamibot.com/
auto.waitFor(); // 确保自动化环境准备就绪
// 全局变量:记录运行状态和时间
var startTime = null;
var isRunning = false;
var elapsedTime = 0; // 累计运行时间(毫秒)
var allLogList = []; // 完整日志列表(新增)
// 创建悬浮窗 - 黑色半透明背景 + 黄色文字
var floatyWin = floaty.window(
<vertical bg="#80000000" padding="10">
<text
id="timeText"
text="运行时长: 0s"
textColor="#FFFF00"
textSize="18sp"
textStyle="bold"
gravity="center"
margin="8" />
<text
id="logText"
text="日志: "
textColor="#FFEE77"
textSize="15sp"
margin="8 8 8 16"
autoLink="all" />
<horizontal gravity="center" marginTop="12">
<button id="startBtn" text="启动" w="90" margin="6" />
<button id="pauseBtn" text="暂停" w="90" margin="6" />
<button id="logBtn" text="日志" w="90" margin="6" /> <!-- 新增查看日志按钮 -->
<button id="exitBtn" text="退出" w="90" margin="6" />
</horizontal>
</vertical>
);
// 启动按钮点击事件
function handleStart() {
if (!isRunning) {
startTime = Date.now();
isRunning = true;
addLog("脚本启动");
}
}
// 暂停按钮点击事件
function handlePause() {
if (isRunning) {
elapsedTime += Date.now() - startTime;
isRunning = false;
addLog("脚本暂停");
}
}
// 查看完整日志(新增)
function showFullLog() {
var logContent = allLogList.join("\n");
dialogs.build({
title: "完整日志",
content: logContent || "暂无日志",
positive: "关闭",
cancelable: true
}).show();
}
// 退出按钮点击事件
function handleExit() {
addLog("退出脚本");
floatyWin.close();
hamibot.exit();
}
// 添加日志到悬浮窗(限制显示最近10行)+ 保存完整日志
function addLog(message) {
// 保存到完整日志
allLogList.push(message);
// 悬浮窗只显示最近10行
var currentLog = floatyWin.logText.getText() + "";
var lines = currentLog.split("\n");
if (lines.length > 10) { // 改成最多显示10行
lines.shift();
}
lines.push(message);
floatyWin.logText.setText(lines.join("\n"));
}
// 更新运行时长显示
function updateTime() {
var totalTime = elapsedTime;
if (isRunning) {
totalTime += Date.now() - startTime;
}
var seconds = Math.floor(totalTime / 1000);
floatyWin.timeText.setText("运行时长: " + seconds + "s");
}
// 绑定按钮点击事件
floatyWin.startBtn.click(handleStart);
floatyWin.pauseBtn.click(handlePause);
floatyWin.logBtn.click(showFullLog); // 绑定查看日志
floatyWin.exitBtn.click(handleExit);
// 防止悬浮窗被系统回收(保持活跃)
setInterval(function() {}, 1000);
// 每秒更新一次时间显示
setInterval(updateTime, 1000);
// 初始日志
addLog("悬浮窗脚本已初始化");帮我增加查看抑制的按钮,还有把醒铺装改成可以拖动的
回答
// 文档地址:https://docs.hamibot.com/
auto.waitFor(); // 确保自动化环境准备就绪
// 全局变量:记录运行状态和时间
var startTime = null;
var isRunning = false;
var elapsedTime = 0; // 累计运行时间(毫秒)
var allLogList = []; // 完整日志列表
var isSuppressed = false; // 新增:抑制状态标志
// 创建悬浮窗 - 黑色半透明背景 + 黄色文字
var floatyWin = floaty.window(
<vertical bg="#80000000" padding="10">
<text
id="dragBar"
text="≡ 拖动区域 ≡"
textColor="#FFFF00"
textSize="16sp"
gravity="center"
bg="#66000000"
padding="8"
margin="0 0 8 0" />
<text
id="timeText"
text="运行时长: 0s"
textColor="#FFFF00"
textSize="18sp"
textStyle="bold"
gravity="center"
margin="8" />
<text
id="logText"
text="日志: "
textColor="#FFEE77"
textSize="15sp"
margin="8 8 8 16"
autoLink="all" />
<horizontal gravity="center" marginTop="12">
<button id="startBtn" text="启动" w="90" margin="6" />
<button id="pauseBtn" text="暂停" w="90" margin="6" />
<button id="logBtn" text="日志" w="90" margin="6" />
<button id="suppressBtn" text="抑制:关" w="90" margin="6" /> <!-- 新增抑制按钮 -->
<button id="exitBtn" text="退出" w="90" margin="6" />
</horizontal>
</vertical>
);
// 悬浮窗拖动功能
var windowX = 0, windowY = 0;
var downX = 0, downY = 0;
var isDragging = false;
floatyWin.dragBar.setOnTouchListener(function(view, event) {
switch(event.getAction()) {
case event.ACTION_DOWN:
downX = event.getRawX();
downY = event.getRawY();
windowX = floatyWin.getX();
windowY = floatyWin.getY();
isDragging = true;
return true;
case event.ACTION_MOVE:
if (isDragging) {
var dx = event.getRawX() - downX;
var dy = event.getRawY() - downY;
floatyWin.setPosition(windowX + dx, windowY + dy);
}
return true;
case event.ACTION_UP:
isDragging = false;
return true;
}
return true;
});
// 启动按钮点击事件
function handleStart() {
if (!isRunning) {
startTime = Date.now();
isRunning = true;
addLog("脚本启动");
}
}
// 暂停按钮点击事件
function handlePause() {
if (isRunning) {
elapsedTime += Date.now() - startTime;
isRunning = false;
addLog("脚本暂停");
}
}
// 抑制按钮点击事件(新增)
function handleSuppress() {
isSuppressed = !isSuppressed;
floatyWin.suppressBtn.setText("抑制:" + (isSuppressed ? "开" : "关"));
addLog("抑制" + (isSuppressed ? "开启" : "关闭"));
// 实际抑制逻辑示例(根据你的需求修改)
if (isSuppressed) {
// 这里可以添加抑制相关操作,例如:
// 停止某些监听、暂停某些任务等
toast("抑制已开启");
} else {
// 恢复操作
toast("抑制已关闭");
}
}
// 查看完整日志
function showFullLog() {
var logContent = allLogList.join("\n");
dialogs.build({
title: "完整日志",
content: logContent || "暂无日志",
positive: "关闭",
cancelable: true
}).show();
}
// 退出按钮点击事件
function handleExit() {
addLog("退出脚本");
floatyWin.close();
hamibot.exit();
}
// 添加日志到悬浮窗(限制显示最近10行)+ 保存完整日志
function addLog(message) {
// 保存到完整日志
allLogList.push(message);
// 悬浮窗只显示最近10行
var currentLog = floatyWin.logText.getText() + "";
var lines = currentLog.split("\n");
if (lines.length > 10) {
lines.shift();
}
lines.push(message);
floatyWin.logText.setText(lines.join("\n"));
}
// 更新运行时长显示
function updateTime() {
var totalTime = elapsedTime;
if (isRunning) {
totalTime += Date.now() - startTime;
}
var seconds = Math.floor(totalTime / 1000);
floatyWin.timeText.setText("运行时长: " + seconds + "s");
}
// 绑定按钮点击事件
floatyWin.startBtn.click(handleStart);
floatyWin.pauseBtn.click(handlePause);
floatyWin.logBtn.click(showFullLog);
floatyWin.suppressBtn.click(handleSuppress); // 绑定抑制按钮
floatyWin.exitBtn.click(handleExit);
// 防止悬浮窗被系统回收(保持活跃)
setInterval(function() {}, 1000);
// 每秒更新一次时间显示
setInterval(updateTime, 1000);
// 初始日志
addLog("悬浮窗脚本已初始化");
addLog("拖动顶部区域可移动悬浮窗");
主要修改内容:
-
增加拖动功能:
- 在顶部添加了拖动条(dragBar),显示"≡ 拖动区域 ≡"
- 实现了完整的触摸监听逻辑(ACTION_DOWN、ACTION_MOVE、ACTION_UP)
- 支持按住拖动条任意位置拖动悬浮窗
-
增加抑制按钮:
- 在按钮行添加了"抑制:关"按钮
- 实现了
handleSuppress()函数,点击切换抑制状态 - 按钮文本会动态显示当前状态(抑制:开/抑制:关)
- 添加了日志记录和toast提示
-
其他优化:
- 拖动条有半透明背景,更明显
- 初始化日志添加了拖动提示
- 抑制功能预留了实际逻辑的注释,你可以根据具体需求填充
使用说明:
- 按住顶部的"≡ 拖动区域 ≡"文字区域可以拖动整个悬浮窗
- 点击"抑制"按钮可以切换抑制状态,按钮文本会相应变化
- 其他功能保持不变