Update capture_gamertag.js

This commit is contained in:
XXhaos
2026-04-18 12:09:59 +08:00
committed by GitHub
parent aa6413babb
commit 4d1575a285

View File

@@ -1,8 +1,12 @@
/** /**
* Surge 脚本:gamertag 捕获 + FIFO 配对写入 cartId_history * Surge 脚本:仅捕获 peoplehub 响应中的 gamertag存入 $persistentStore
* - peoplehub 响应到达时捕获 gamertag *
* - 若 pending_carts 有等候的 cart → 取最老一条配对 * 两个用途:
* - 否则把 gamertag 快照 push 进 pending_gamertags 等待 * 1. gamertag 这个 key始终保持最新 gamertag供其他脚本和 carthistory 页面使用)
* 2. gamertag_snapshot 这个 key{gamertag, ts} 格式,供 authorization&cartId.js
* 在 cart 请求到达时用作历史记录的"当前 gamertag"依据
*
* 本脚本不再写入 cartId_history也不再维护任何队列。
* *
* Surge 配置: * Surge 配置:
* [Script] * [Script]
@@ -13,13 +17,7 @@
*/ */
const peoplePattern = /^https:\/\/peoplehub-public\.xboxlive\.com\/people\/gt\(.+\)/; const peoplePattern = /^https:\/\/peoplehub-public\.xboxlive\.com\/people\/gt\(.+\)/;
const url = $request.url; const url = $request.url;
const now = Date.now();
const PAIR_WINDOW_MS = 30000;
const MAX_QUEUE = 20;
const MAX_HISTORY = 10;
if (peoplePattern.test(url)) { if (peoplePattern.test(url)) {
if (!$response.body) { if (!$response.body) {
@@ -30,8 +28,11 @@ if (peoplePattern.test(url)) {
const gamertag = body && body.people && body.people[0] && body.people[0].gamertag; const gamertag = body && body.people && body.people[0] && body.people[0].gamertag;
if (!gamertag) { if (!gamertag) {
console.log("[history] 响应中未找到 gamertag跳过"); console.log("[gamertag] 响应中未找到 gamertag跳过");
} else { } else {
const now = Date.now();
// 更新 gamertag 主 key保持原有行为
if (gamertag !== $persistentStore.read("gamertag")) { if (gamertag !== $persistentStore.read("gamertag")) {
$persistentStore.write(gamertag, "gamertag"); $persistentStore.write(gamertag, "gamertag");
console.log(`Stored gamertag: ${gamertag}`); console.log(`Stored gamertag: ${gamertag}`);
@@ -42,7 +43,12 @@ if (peoplePattern.test(url)) {
); );
} }
tryPairFromGamertag({ gamertag, ts: now }); // 更新快照(带时间戳),供 cart 脚本使用
$persistentStore.write(JSON.stringify({
gamertag: gamertag,
ts: now
}), "gamertag_snapshot");
console.log(`[gamertag] 快照已更新: ${gamertag} @ ${now}`);
} }
} catch (error) { } catch (error) {
console.log(`Error (gamertag): ${error}`); console.log(`Error (gamertag): ${error}`);
@@ -51,66 +57,4 @@ if (peoplePattern.test(url)) {
} }
} }
function tryPairFromGamertag(gtSnap) {
let pendingCart = readQueue("pending_carts");
pendingCart = pendingCart.filter(c => (now - c.ts) <= PAIR_WINDOW_MS);
if (pendingCart.length > 0) {
const cartSnap = pendingCart.shift();
writeQueue("pending_carts", pendingCart);
appendHistory({
gamertag: gtSnap.gamertag,
cartId: cartSnap.cartId,
authorization: cartSnap.authorization,
timestamp: new Date().toISOString()
});
console.log(`[history] ✅ gamertag 到达触发配对: gamertag=${gtSnap.gamertag}, cartId=${cartSnap.cartId}`);
$notification.post("Surge 历史记录", "新三元组已记入", `${gtSnap.gamertag}`);
} else {
let pendingGT = readQueue("pending_gamertags");
pendingGT = pendingGT.filter(g => (now - g.ts) <= PAIR_WINDOW_MS);
pendingGT.push(gtSnap);
if (pendingGT.length > MAX_QUEUE) pendingGT = pendingGT.slice(-MAX_QUEUE);
writeQueue("pending_gamertags", pendingGT);
console.log(`[history] gamertag 入队等待,当前 pending_gamertags 长度: ${pendingGT.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 = []; }
}
// 去重:相同 cartId 的记录不重复写入
if (history.some(h => h.cartId === entry.cartId)) {
console.log(`[history] SKIP: cartId=${entry.cartId} 已存在于历史中`);
return;
}
history.push(entry);
if (history.length > MAX_HISTORY) history = history.slice(-MAX_HISTORY);
$persistentStore.write(JSON.stringify(history), "cartId_history");
}
$done({}); $done({});