/** * ParkingRecorder API 版(Surge / Loon / Quantumult X 通用) * 访问 http://api.surge.run/ParkingRecorder 时: * 1. 写入当前时间到持久化存储 * 2. 本地推送通知 * 3. 返回 {"ok":true,"time":"xxxx"}(快捷指令不会弹“网络中断”) */ (() => { // ========== 环境检测 ========== const isQuantumultX = typeof $task !== "undefined"; const isSurge = typeof $httpClient !== "undefined" && typeof $notification !== "undefined"; const isLoon = typeof $loon !== "undefined"; // ========== 工具函数封装 ========== // 通知 function notify(title, subtitle, message) { if (isQuantumultX) { $notify(title, subtitle, message); } else if (isSurge || isLoon) { $notification.post(title, subtitle, message); } } // 写入持久化 function writeStore(value, key) { if (isQuantumultX) { // QX 返回布尔值 return $prefs.setValueForKey(value, key); } else if (isSurge || isLoon) { // Surge / Loon 共用 return $persistentStore.write(value, key); } else { return false; } } // 统一结束 HTTP 响应 function done(bodyObj) { const json = JSON.stringify(bodyObj); if (isQuantumultX) { // Quantumult X 写法 $done({ status: "HTTP/1.1 200 OK", headers: { "Content-Type": "application/json; charset=utf-8" }, body: json }); } else if (isSurge || isLoon) { // Surge / Loon 写法 $done({ response: { status: 200, headers: { "Content-Type": "application/json; charset=utf-8" }, body: json } }); } else { // 其他环境兜底 try { console.log(json); } catch (e) {} if (typeof $done === "function") $done(); } } // ========== 主逻辑 ========== try { const key = "ParkingRecorder"; // 获取当前时间(格式:YYYY-MM-DD HH:mm:ss) const now = new Date(); const timeString = now .toLocaleString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false }) .replace(/\//g, "-"); // 写入持久化 const success = writeStore(timeString, key); // 本地通知 notify( success ? "🚗 停车时间已更新" : "❌ 停车时间写入失败", timeString, isQuantumultX ? "Quantumult X" : isLoon ? "Loon" : isSurge ? "Surge" : "" ); // 返回给快捷指令 done({ ok: !!success, time: timeString, key }); } catch (err) { // 兜底:永远返回 200,避免快捷指令报“网络中断” notify("❌ 停车脚本异常", String(err), ""); done({ ok: false, error: String(err) }); } })();