Web Dashboard Optimizer
by @zqh07-bit
Create smooth, stable web monitoring dashboards with smart refresh, scroll position retention, modal interaction, auto-restart, and responsive design.
clawhub install web-dashboard-optimizer📖 About This Skill
Web 界面交互优化技能
为 AI Agent 创建流畅、稳定的 Web 监控面板界面。
功能特性
适用场景
快速开始
1. 创建基础 HTML 文件
cd /workspace/tools
cat > dashboard.html << 'EOF'
监控面板
📊 监控面板
自动刷新 (60 秒)
-
统计 1
-
统计 2
📋 列表
EOF
2. 创建 Node.js 服务器
cat > server.js << 'EOF'
const http = require('http');
const fs = require('fs');
const path = require('path');const PORT = process.env.PORT || 18790;
// 读取 HTML 文件
const htmlPath = path.join(__dirname, 'dashboard.html');
const html = fs.readFileSync(htmlPath, 'utf-8');
// 模拟数据
function getData() {
return {
timestamp: Date.now(),
items: [
{ id: 1, name: '任务 1', status: 'success' },
{ id: 2, name: '任务 2', status: 'pending' },
]
};
}
const server = http.createServer((req, res) => {
// CORS
res.setHeader('Access-Control-Allow-Origin', '*');
if (req.url === '/' || req.url === '/index.html') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
return;
}
if (req.url === '/api/data') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(getData()));
return;
}
res.writeHead(404);
res.end('Not Found');
});
server.listen(PORT, () => {
console.log(📊 监控面板已启动);
console.log( 访问地址:http://localhost:${PORT});
console.log( API: http://localhost:${PORT}/api/data);
});
EOF
3. 创建带自动重启的启动脚本
cat > run-monitor.sh << 'EOF'
#!/bin/bashSCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_DIR="/tmp/openclaw-monitor"
PID_FILE="$LOG_DIR/monitor.pid"
LOG_FILE="$LOG_DIR/monitor.log"
mkdir -p "$LOG_DIR"
检查是否已在运行
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
echo "✅ 监控面板已在运行 (PID: $PID)"
exit 0
fi
fi启动服务(带自动重启)
echo "🚀 启动监控面板..."
cd "$SCRIPT_DIR"(
while true; do
node server.js >> "$LOG_FILE" 2>&1
echo "⚠️ 服务意外停止,5 秒后重启..." >> "$LOG_FILE"
sleep 5
done
) > /dev/null 2>&1 &
echo $! > "$PID_FILE"
sleep 3
if ps -p $(cat "$PID_FILE") > /dev/null 2>&1; then
echo "✅ 监控面板已启动"
echo " 访问地址:http://localhost:18790"
echo " 日志文件:$LOG_FILE"
echo " 进程 PID: $(cat $PID_FILE)"
else
echo "❌ 启动失败"
rm -f "$PID_FILE"
fi
EOF
chmod +x run-monitor.sh
4. 运行
./run-monitor.sh
访问:http://localhost:18790
关键优化点
1. 智能刷新
// 只在数据变化时重新渲染
const hasChanges = newDataHash !== lastDataHash;
if (hasChanges) {
render(data);
// 恢复滚动位置
window.scrollTo(0, scrollY);
}
2. 滚动位置保持
// 刷新前保存
const scrollY = window.scrollY;// 渲染后恢复
window.scrollTo(0, scrollY);
3. 弹窗交互
// 打开时保存位置
const scrollY = window.scrollY;
document.body.style.top = -scrollY + 'px';
document.body.classList.add('modal-open');// 关闭时恢复
const scrollY = parseInt(document.body.style.top || '0');
document.body.classList.remove('modal-open');
window.scrollTo(0, Math.abs(scrollY));
4. 服务自动重启
while true; do
node server.js
sleep 5 # 5 秒后自动重启
done
自定义
修改数据源
编辑 server.js 中的 getData() 函数:
function getData() {
// 从数据库、API 或文件读取数据
const data = fs.readFileSync('/path/to/data.json');
return JSON.parse(data);
}
修改样式
编辑 dashboard.html 中的 部分。
修改刷新间隔
// 改为 30 秒
refreshInterval = setInterval(loadData, 30000);
最佳实践
1. 数据哈希对比:避免不必要的重新渲染 2. 滚动位置保存:提升用户体验 3. 弹窗位置管理:防止页面跳动 4. 自动重启:确保服务持续在线 5. 日志记录:便于问题排查
故障排查
服务无法启动
# 查看日志
tail -f /tmp/openclaw-monitor/monitor.log检查端口占用
lsof -ti:18790 | xargs kill -9重新启动
./run-monitor.sh
页面空白
1. 检查浏览器控制台错误 2. 确认服务器正在运行 3. 检查 HTML 文件路径
自动刷新导致跳动
确保 loadData() 函数中保存和恢复了滚动位置。
扩展功能
添加搜索过滤
function filterTasks() {
const searchText = document.getElementById('searchInput').value.toLowerCase();
filteredTasks = allTasks.filter(task =>
task.name.toLowerCase().includes(searchText)
);
renderTasks(filteredTasks);
}
添加状态筛选
添加数据分组
const taskGroups = {
'stock': { name: '📈 股票监控', keywords: ['股票', '持仓'] },
'news': { name: '📰 新闻资讯', keywords: ['新闻', '简报'] }
};function getTaskGroup(taskName) {
for (const [group, config] of Object.entries(taskGroups)) {
if (config.keywords.some(k => taskName.includes(k))) {
return group;
}
}
return 'other';
}
总结
这个技能提供了:
可以直接用于创建各种监控面板,无需重复处理交互细节。