Update authorization&cartId.js

This commit is contained in:
XXhaos
2026-04-18 13:03:06 +08:00
committed by GitHub
parent 4ac2f7719d
commit 334e1f6f6f

View File

@@ -1,23 +1,18 @@
/** /**
* Surge 脚本:捕获 cart 请求的 Authorization 和 cartId * Surge 脚本:捕获 cart 请求的 Authorization 和 cartId
* 并在同一时刻根据最近捕获的 gamertag 写入 cartId_history
* *
* 保留原有功能: * 功能:
* - 始终在 $persistentStore 中维护最新的 authorization 和 cartId * - 保持原有:始终在 $persistentStore 中维护最新的 authorization 和 cartId
* - 新增:把每次捕获追加到 cart_records 数组cartId 去重)
* *
* 历史记录逻辑: * cart_records 结构:[{cartId, authorization, ts}, ...]
* - 读取 gamertag_snapshot由 gamertag 脚本写入,格式 {gamertag, ts} * - 匹配由网页脚本动态完成,本脚本不做任何配对
* - 若快照时间在 GAMERTAG_FRESHNESS_MS 窗口内 → 用它 + 本次 cartId/auth 写入历史
* - 若快照过旧或不存在 → 跳过,不写入(宁可漏记也不错记)
* - 相同 cartId 的记录不重复写入
*/ */
const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/Cart\/eligibilityCheck\?/; const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/Cart\/eligibilityCheck\?/;
const url = $request.url; const url = $request.url;
// gamertag 快照"新鲜度"窗口:超出此时间认为不可信 const MAX_RECORDS = 20; // cart 记录保留最近 20 条
const GAMERTAG_FRESHNESS_MS = 10000; // 10 秒
const MAX_HISTORY = 10;
if ($request.method === "PUT" && pattern.test(url)) { if ($request.method === "PUT" && pattern.test(url)) {
try { try {
@@ -36,9 +31,9 @@ if ($request.method === "PUT" && pattern.test(url)) {
console.log(`Stored cartId: ${cartId}`); console.log(`Stored cartId: ${cartId}`);
} }
// 尝试写入历史 // 新增:追加到 cart_recordscartId 去重)
if (cartId && authorization) { if (cartId && authorization) {
tryAppendHistory(cartId, authorization, now); appendCartRecord({ cartId, authorization, ts: now });
} }
$notification.post( $notification.post(
@@ -55,74 +50,28 @@ if ($request.method === "PUT" && pattern.test(url)) {
} }
} }
function tryAppendHistory(cartId, authorization, now) { function appendCartRecord(entry) {
// 读取 gamertag 快照 let records = [];
const raw = $persistentStore.read("gamertag_snapshot"); const raw = $persistentStore.read("cart_records");
if (!raw) { if (raw) {
console.log(`[history] SKIP: gamertag 快照不存在gamertag 脚本还没捕获过)`);
return;
}
let snapshot;
try { try {
snapshot = JSON.parse(raw); const parsed = JSON.parse(raw);
} catch (e) { if (Array.isArray(parsed)) records = parsed;
console.log(`[history] SKIP: gamertag 快照解析失败`); } catch (e) { records = []; }
return;
}
const { gamertag, ts } = snapshot || {};
if (!gamertag || !ts) {
console.log(`[history] SKIP: gamertag 快照字段不完整`);
return;
}
// 检查快照新鲜度
const age = now - ts;
console.log(`[history] gamertag 快照: ${gamertag}, 距今 ${age}ms`);
if (age > GAMERTAG_FRESHNESS_MS) {
console.log(`[history] SKIP: gamertag 快照过旧 (${age}ms > ${GAMERTAG_FRESHNESS_MS}ms)`);
return;
}
if (age < 0) {
console.log(`[history] SKIP: gamertag 快照时间异常(在未来)`);
return;
}
// 读取历史
let history = [];
const histRaw = $persistentStore.read("cartId_history");
if (histRaw) {
try {
const parsed = JSON.parse(histRaw);
if (Array.isArray(parsed)) history = parsed;
} catch (e) { history = []; }
} }
// 去重:相同 cartId 不重复记录 // 去重:相同 cartId 不重复记录
if (history.some(h => h.cartId === cartId)) { if (records.some(r => r.cartId === entry.cartId)) {
console.log(`[history] SKIP: cartId=${cartId} 已存在于历史中`); console.log(`[cart] SKIP: cartId=${entry.cartId} 已存在`);
return; return;
} }
const entry = { records.push(entry);
gamertag, if (records.length > MAX_RECORDS) records = records.slice(-MAX_RECORDS);
cartId, $persistentStore.write(JSON.stringify(records), "cart_records");
authorization,
timestamp: new Date().toISOString()
};
history.push(entry); console.log(`[cart] ✅ 已记录 cartId=${entry.cartId}, total=${records.length}`);
if (history.length > MAX_HISTORY) history = history.slice(-MAX_HISTORY); $notification.post("Surge 历史记录", "已记录 cart 信息", "");
$persistentStore.write(JSON.stringify(history), "cartId_history");
console.log(`[history] ✅ 写入成功! gamertag=${gamertag}, cartId=${cartId}, total=${history.length}`);
$notification.post(
"Surge 历史记录",
"新三元组已记入",
`${gamertag}`
);
} }
$done({}); $done({});