// ==UserScript==
// @name 游戏盈亏监控
// @namespace https://greasyfork.org/users/your-id
// @version 2.0.0
// @description 监控游戏平台的用户盈亏数据,当盈利/亏损超过设定阈值时弹窗提醒。适用于后台管理系统。大数据量优化的高效监控脚本,支持并行处理
// @author Cisco
// @match https://7777m.topcms.org/*
// @match https://*.topcms.org/*
// @icon https://7777m.topcms.org/favicon.ico
// @license MIT
// @grant GM_notification
// @grant GM_xmlhttpRequest
// @run-at document-end
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
// 高性能配置
const config = {
batchSize: 50, // 每批处理数量
parallelRequests: 3, // 并行请求数
profitThreshold: null,
lossThreshold: null,
monitoring: false,
currentBatch: 0,
totalItems: 0,
apiEndpoint: '/api/getUserProfits' // 假设有直接API接口
};
// 现代UI控制面板
function createControlPanel() {
const panel = document.createElement('div');
panel.id = 'monitorPanel';
panel.style.cssText = `
position: fixed; top: 20px; right: 20px; z-index: 9999;
background: #fff; padding: 15px; border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15); width: 300px;
font-family: system-ui, sans-serif;
`;
panel.innerHTML = `
⚡ 极速盈亏监控
`;
document.body.appendChild(panel);
document.getElementById('toggleMonitor').addEventListener('click', toggleMonitoring);
}
// 高性能监控切换
function toggleMonitoring() {
config.profitThreshold = parseFloat(document.getElementById('profitThresholdInput').value) || null;
config.lossThreshold = Math.abs(parseFloat(document.getElementById('lossThresholdInput').value)) || null;
if (!config.profitThreshold && !config.lossThreshold) {
alert('请至少设置一个阈值');
return;
}
config.monitoring = !config.monitoring;
const btn = document.getElementById('toggleMonitor');
if (config.monitoring) {
btn.textContent = '停止监控';
btn.style.background = '#F56C6C';
startHighPerformanceMonitoring();
} else {
btn.textContent = '开始监控';
btn.style.background = '#409EFF';
}
}
// 核心优化:批量API请求 + 虚拟滚动
async function startHighPerformanceMonitoring() {
try {
// 1. 获取数据总量
const total = await fetchTotalCount();
config.totalItems = total;
document.getElementById('total').textContent = total;
// 2. 计算批次
const totalBatches = Math.ceil(total / config.batchSize);
// 3. 并行处理批次
const batchPromises = [];
for (let i = 0; i < Math.min(config.parallelRequests, totalBatches); i++) {
batchPromises.push(processBatch(i));
}
await Promise.all(batchPromises);
// 4. 循环监控
if (config.monitoring) {
setTimeout(startHighPerformanceMonitoring, config.checkInterval);
}
} catch (error) {
console.error('监控出错:', error);
notify(`监控失败: ${error.message}`, 'error');
}
}
// 批量处理数据
async function processBatch(batchIndex) {
while (batchIndex * config.batchSize < config.totalItems && config.monitoring) {
const start = batchIndex * config.batchSize;
const end = Math.min(start + config.batchSize, config.totalItems);
try {
// 1. 获取批量数据
const users = await fetchBatchData(start, end);
// 2. 快速分析数据
const results = analyzeBatch(users);
// 3. 更新UI
updateProgress(start + users.length);
// 4. 处理超标情况
handleThresholdBreaches(results);
batchIndex += config.parallelRequests;
} catch (error) {
console.error(`批次 ${batchIndex} 处理失败:`, error);
await new Promise(resolve => setTimeout(resolve, 2000)); // 错误延迟
}
}
}
// 模拟API请求
async function fetchBatchData(start, end) {
return new Promise((resolve) => {
// 实际替换为真实API调用
GM_xmlhttpRequest({
method: 'POST',
url: config.apiEndpoint,
data: JSON.stringify({ start, end }),
headers: {
'Content-Type': 'application/json',
'X-Token': 'YOUR_AUTH_TOKEN'
},
onload: (res) => {
try {
const data = JSON.parse(res.responseText);
resolve(data.users || []);
} catch {
resolve([]);
}
},
onerror: () => resolve([])
});
});
}
// 快速批量分析
function analyzeBatch(users) {
return users.map(user => {
const profit = parseFloat(user.profit) || 0;
const isExceeded = (profit > 0 && config.profitThreshold && profit >= config.profitThreshold) ||
(profit < 0 && config.lossThreshold && Math.abs(profit) >= config.lossThreshold);
return {
id: user.id,
name: user.name,
profit,
isExceeded
};
});
}
// 处理超标数据
function handleThresholdBreaches(results) {
results.filter(r => r.isExceeded).forEach(user => {
const type = user.profit > 0 ? 'profit' : 'loss';
notify(`用户 ${user.name} ${type === 'profit' ? '盈利' : '亏损'}超标: ${user.profit}`, type);
});
}
// 进度更新
function updateProgress(processed) {
document.getElementById('progress').textContent = processed;
const percent = Math.round((processed / config.totalItems) * 100);
document.getElementById('progressBar').style.width = `${percent}%`;
}
// 通知优化
function notify(message, type) {
const colors = { profit: '#67C23A', loss: '#F56C6C', error: '#E6A23C' };
console.log(`%c${message}`, `color: ${colors[type] || '#409EFF'}; font-weight:bold`);
if (typeof GM_notification !== 'undefined') {
GM_notification({
title: type === 'profit' ? '盈利报警' : type === 'loss' ? '亏损报警' : '系统通知',
text: message,
timeout: 5000
});
}
}
// 初始化
function init() {
if (!document.getElementById('monitorPanel')) {
createControlPanel();
console.log('极速监控脚本已加载');
}
}
// 启动
if (document.readyState === 'complete') init();
else window.addEventListener('load', init);
})();