From ff11ea2579e7107aed24b56eef86ef3efcde496a Mon Sep 17 00:00:00 2001 From: XXhaos Date: Sat, 18 Apr 2026 11:25:19 +0800 Subject: [PATCH] Update authorization&cartId.js --- Scripts/authorization&cartId.js | 81 +++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/Scripts/authorization&cartId.js b/Scripts/authorization&cartId.js index da7bd67..dc85a0a 100644 --- a/Scripts/authorization&cartId.js +++ b/Scripts/authorization&cartId.js @@ -1,32 +1,38 @@ /** - * Surge 脚本:捕获特定 URL 请求的 Authorization 和 cartId(仅限 PUT 请求) - * 并存储到持久化存储($persistentStore),只有在成功捕获到新值时才更新 + * Surge 脚本:捕获 cart 请求的 Authorization 和 cartId + * - 保留原有功能:始终在 $persistentStore 中维护最新的 authorization 和 cartId + * - 新增功能:FIFO 配对写入 cartId_history + * · 若 pending_gamertags 有等候的 gamertag → 取最老一条配对 + * · 否则把 cart 快照 push 进 pending_carts 等待 */ const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/Cart\/eligibilityCheck\?/; const url = $request.url; -// 仅处理 PUT 请求 +const PAIR_WINDOW_MS = 30000; +const MAX_QUEUE = 20; +const MAX_HISTORY = 100; + if ($request.method === "PUT" && pattern.test(url)) { try { - // 获取 Authorization 请求头的值 const authorization = $request.headers['authorization']; - // 解析 URL 以提取 cartId 参数 const urlObj = new URL(url); const cartId = urlObj.searchParams.get('cartId'); + const now = Date.now(); - // 只有在成功捕获到新值时,才更新 $persistentStore 中的值 if (authorization && authorization !== $persistentStore.read("authorization")) { $persistentStore.write(authorization, "authorization"); console.log(`Stored authorization: ${authorization}`); } - if (cartId && cartId !== $persistentStore.read("cartId")) { $persistentStore.write(cartId, "cartId"); console.log(`Stored cartId: ${cartId}`); } - - // 发送通知,成功捕获并存储 Authorization 和 CartId + + if (cartId && authorization) { + tryPairFromCart({ cartId, authorization, ts: now }); + } + $notification.post( "Surge 信息存储", "已捕获并存储 authorization 和 cartId" @@ -41,4 +47,61 @@ if ($request.method === "PUT" && pattern.test(url)) { } } +function tryPairFromCart(cartSnap) { + const now = cartSnap.ts; + + let pendingGT = readQueue("pending_gamertags"); + pendingGT = pendingGT.filter(g => (now - g.ts) <= PAIR_WINDOW_MS); + + if (pendingGT.length > 0) { + const gtSnap = pendingGT.shift(); + writeQueue("pending_gamertags", pendingGT); + + appendHistory({ + gamertag: gtSnap.gamertag, + cartId: cartSnap.cartId, + authorization: cartSnap.authorization, + timestamp: new Date().toISOString() + }); + + console.log(`[history] ✅ cart 到达触发配对: gamertag=${gtSnap.gamertag}, cartId=${cartSnap.cartId}`); + $notification.post("Surge 历史记录", "新三元组已记入", `${gtSnap.gamertag}`); + } else { + let pendingCart = readQueue("pending_carts"); + pendingCart = pendingCart.filter(c => (now - c.ts) <= PAIR_WINDOW_MS); + pendingCart.push(cartSnap); + if (pendingCart.length > MAX_QUEUE) pendingCart = pendingCart.slice(-MAX_QUEUE); + writeQueue("pending_carts", pendingCart); + + console.log(`[history] cart 入队等待,当前 pending_carts 长度: ${pendingCart.length}`); + } +} + +function readQueue(key) { + const raw = $persistentStore.read(key); + if (!raw) return []; + try { + const parsed = JSON.parse(raw); + return Array.isArray(parsed) ? parsed : []; + } catch (e) { return []; } +} + +function writeQueue(key, arr) { + $persistentStore.write(JSON.stringify(arr), key); +} + +function appendHistory(entry) { + let history = []; + const raw = $persistentStore.read("cartId_history"); + if (raw) { + try { + const parsed = JSON.parse(raw); + if (Array.isArray(parsed)) history = parsed; + } catch (e) { history = []; } + } + history.push(entry); + if (history.length > MAX_HISTORY) history = history.slice(-MAX_HISTORY); + $persistentStore.write(JSON.stringify(history), "cartId_history"); +} + $done({});