// ==UserScript== // @name 查询 bandwagonhost 的 VPS流量 // @namespace bwg_vps_data // @version 1.0 // @description 先填写 用户配置 id和 key,使用 bandwagonhost 官方 API查询VPS流量使用情况(后台脚本,每日9点执行) // @author zip11 // @grant GM_notification // @grant GM_xmlhttpRequest // @grant GM_setValue // @grant GM_getValue // @grant GM_getConfig // @connect api.64clouds.com // @run-at document-end // @background // @cron 0 9 * * * // @storageName vps_api_data_sc_bg // ==/UserScript== /* ==UserConfig== group1: veid: title: VEID description: 64clouds VPS的VEID type: text default: "" password: true api_key: title: API Key description: 64clouds VPS的API Key type: text default: "" password: true enable: title: 启用通知 description: 查询后发送系统通知 type: checkbox default: true ==/UserConfig== */ /** * 查询VPS流量 - ScriptCat后台版本 * 使用方法:在ScriptCat配置面板中填写VEID和API Key,每天9点自动执行 * API: https://api.64clouds.com/v1/getServiceInfo */ (function() { 'use strict'; // ==================== [模块1] HTTP请求封装 ==================== /** * 封装GM_xmlhttpRequest为Promise * @param {Object} options - 请求配置 * @return {Promise} Promise对象 */ function httpRequest(options) { return new Promise(function(resolve, reject) { options.onload = function(response) { resolve(response); }; options.onerror = function(response) { reject(response); }; GM_xmlhttpRequest(options); }); } /** * 发送POST请求 * @param {string} url - 请求地址 * @param {string} data - 请求数据 * @return {Promise} Promise对象 */ function postRequest(url, data) { return httpRequest({ method: 'POST', url: url, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: data }); } // ==================== [模块2] 工具函数 ==================== /** * 字节转GB * @param {number} bytes - 字节 * @return {string} GB字符串(2位小数) */ function bytesToGB(bytes) { return (bytes / (1024 * 1024 * 1024)).toFixed(2); } /** * 格式化Unix时间戳 * @param {number} timestamp - Unix时间戳(秒) * @return {string} 格式化日期时间 */ function formatDate(timestamp) { if (!timestamp) return '未知'; var date = new Date(timestamp * 1000); var y = date.getFullYear(); var m = ('0' + (date.getMonth() + 1)).slice(-2); var d = ('0' + date.getDate()).slice(-2); var h = ('0' + date.getHours()).slice(-2); var min = ('0' + date.getMinutes()).slice(-2); return y + '-' + m + '-' + d + ' ' + h + ':' + min; } // ==================== [模块3] 配置读取 ==================== /** * 获取配置 * @return {Object} 配置对象{veid, api_key, enable} */ function getConfig() { return { veid: GM_getValue('group1.veid', ''), api_key: GM_getValue('group1.api_key', ''), enable: GM_getValue('group1.enable', true) }; } /** * 检查是否已配置 * @param {Object} cfg - 配置对象 * @return {boolean} 是否已配置 */ function hasConfig(cfg) { return cfg.veid && cfg.api_key; } // ==================== [模块4] 通知显示 ==================== /** * 显示通知 * @param {string} title - 标题 * @param {string} text - 内容 * @param {boolean} enable - 是否启用 */ function notify(title, text, enable) { if (!enable) return; GM_notification({ title: title, text: text, timeout: 195000 }); } /** * 显示错误通知 * @param {string} title - 标题 * @param {string} text - 内容 * @param {boolean} enable - 是否启用 */ function notifyError(title, text, enable) { if (!enable) return; GM_notification({ title: title, text: text, timeout: 10000 }); } // ==================== [模块5] 数据处理 ==================== /** * 解析API返回数据 * @param {string} responseText - API响应文本 * @return {Object} 流量数据对象 */ function parseVPSData(responseText) { var data = JSON.parse(responseText); if (data.error !== 0) { throw new Error(data.message || 'API错误'); } var usedBytes = data.data_counter || 0; var limitBytes = data.plan_monthly_data || 1; return { usedGB: bytesToGB(usedBytes), limitGB: bytesToGB(limitBytes), percentage: ((usedBytes / limitBytes) * 100).toFixed(2), nextReset: formatDate(data.data_next_reset) }; } /** * 构建通知消息 * @param {Object} data - 流量数据对象 * @return {string} 通知消息文本 */ function buildMessage(data) { return '本月已使用流量: ' + data.usedGB + ' GB\n' + '本月流量限额: ' + data.limitGB + ' GB\n' + '流量下次重置时间: ' + data.nextReset + '\n' + '使用占比: ' + data.percentage + '%'; } // ==================== [模块6] 主函数 ==================== /** * 查询VPS流量 - 入口函数 * 读取配置→发送请求→解析数据→显示通知 */ function queryVPSData() { var cfg = getConfig(); // 检查配置 if (!hasConfig(cfg)) { notifyError('查询VPS流量', '请先配置VEID和API_KEY', cfg.enable); console.log('⚠️ 未配置VEID或API Key'); return; } // 发送请求 var requestData = 'veid=' + encodeURIComponent(cfg.veid) + '&api_key=' + encodeURIComponent(cfg.api_key); postRequest('https://api.64clouds.com/v1/getServiceInfo', requestData) .then(function(response) { // 解析数据 var data = parseVPSData(response.responseText); var message = buildMessage(data); // 显示结果 notify('查询VPS流量', message, cfg.enable); console.log('📊 VPS流量:', data.usedGB, '/', data.limitGB, 'GB'); }) .catch(function(error) { notifyError('查询VPS流量', '请求失败: ' + error.message, cfg.enable); console.log('❌ 请求失败:', error.message); }); } // 立即执行(后台脚本加载时) queryVPSData(); })();