更新 Scripts/CartParameter.js

This commit is contained in:
2026-06-02 19:12:54 +09:00
parent 906c33aaf0
commit 85266685dd

View File

@@ -7,55 +7,20 @@
*/ */
// 修改正则表达式,捕获新的请求 URL // 修改正则表达式,捕获新的请求 URL
const patterns = [ const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/cart\/loadCart\?/;
// XboxWeb 老路径 const url = $request.url;
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/cart\/loadCart\?/i,
// StoreWeb 新路径
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/cart\/v1\.0\/cart\/loadCart\?/i,
// 兼容可能出现的版本路径
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/cart\/v\d+\.\d+\/cart\/loadCart\?/i,
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/v\d+\.\d+\/cart\/loadCart\?/i,
// checkout / purchase 流程里可能出现的 cart 请求
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/.*\/cart\/loadCart\?/i,
// 更宽松兜底:只要是该域名下的 loadCart
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/.*loadCart\?/i
];
const url = $request.url || "";
const matched = patterns.some(p => p.test(url));
// 兼容不同平台 Header 大小写差异
function getHeader(name) {
const headers = $request.headers || {};
const target = name.toLowerCase();
for (const key in headers) {
if (key.toLowerCase() === target) {
return headers[key];
}
}
return undefined;
}
// 判断是否是 PUT 请求并且 URL 匹配 // 判断是否是 PUT 请求并且 URL 匹配
if (($request.method || "").toUpperCase() === "PUT" && matched) { if ($request.method === "PUT" && pattern.test(url)) {
try { try {
console.log(`Matched cart request: ${url}`);
// 从请求头中获取所需参数 // 从请求头中获取所需参数
const xAuthorizationMuid = getHeader('x-authorization-muid'); const xAuthorizationMuid = $request.headers['x-authorization-muid'];
const msCv = getHeader('ms-cv'); const msCv = $request.headers['ms-cv'];
const xMsVectorId = getHeader('x-ms-vector-id'); const xMsVectorId = $request.headers['x-ms-vector-id'];
const xMsReferenceId = getHeader('x-ms-reference-id'); const xMsReferenceId = $request.headers['x-ms-reference-id'];
// 新增参数获取 // 新增参数获取
const xMsTrackingId = getHeader('x-ms-tracking-id'); const xMsTrackingId = $request.headers['x-ms-tracking-id'];
const xMsCorrelationId = getHeader('x-ms-correlation-id'); const xMsCorrelationId = $request.headers['x-ms-correlation-id'];
const xValidationField1 = getHeader('x-validation-field-1');
// 更新存储逻辑(仅在新值有效时更新) // 更新存储逻辑(仅在新值有效时更新)
let hasUpdate = false; let hasUpdate = false;
@@ -88,27 +53,20 @@ if (($request.method || "").toUpperCase() === "PUT" && matched) {
hasUpdate = true; hasUpdate = true;
} }
// 5. x-ms-tracking-id // 5. [新增] x-ms-tracking-id
if (xMsTrackingId && xMsTrackingId !== $persistentStore.read("cart-x-ms-tracking-id")) { if (xMsTrackingId && xMsTrackingId !== $persistentStore.read("cart-x-ms-tracking-id")) {
$persistentStore.write(xMsTrackingId, "cart-x-ms-tracking-id"); $persistentStore.write(xMsTrackingId, "cart-x-ms-tracking-id");
console.log(`Stored x-ms-tracking-id: ${xMsTrackingId}`); console.log(`Stored x-ms-tracking-id: ${xMsTrackingId}`);
hasUpdate = true; hasUpdate = true;
} }
// 6. x-ms-correlation-id // 6. [新增] x-ms-correlation-id
if (xMsCorrelationId && xMsCorrelationId !== $persistentStore.read("cart-x-ms-correlation-id")) { if (xMsCorrelationId && xMsCorrelationId !== $persistentStore.read("cart-x-ms-correlation-id")) {
$persistentStore.write(xMsCorrelationId, "cart-x-ms-correlation-id"); $persistentStore.write(xMsCorrelationId, "cart-x-ms-correlation-id");
console.log(`Stored x-ms-correlation-id: ${xMsCorrelationId}`); console.log(`Stored x-ms-correlation-id: ${xMsCorrelationId}`);
hasUpdate = true; hasUpdate = true;
} }
// 7. x-validation-field-1
if (xValidationField1 && xValidationField1 !== $persistentStore.read("cart-x-validation-field-1")) {
$persistentStore.write(xValidationField1, "cart-x-validation-field-1");
console.log(`Stored x-validation-field-1: ${xValidationField1}`);
hasUpdate = true;
}
// 仅在成功捕获新值时发送通知(如果需要开启通知,取消下方注释) // 仅在成功捕获新值时发送通知(如果需要开启通知,取消下方注释)
// if (hasUpdate) { // if (hasUpdate) {
// $notification.post( // $notification.post(
@@ -120,7 +78,12 @@ if (($request.method || "").toUpperCase() === "PUT" && matched) {
} catch (error) { } catch (error) {
console.log(`Error capturing parameters: ${error}`); console.log(`Error capturing parameters: ${error}`);
// $notification.post(
// "Surge 脚本错误",
// "无法捕获所需购物车参数",
// `${error}`
// );
} }
} }
$done({}); $done({});