diff --git a/Scripts/CartParameter.js b/Scripts/CartParameter.js index fe1b25a..f5ee7a1 100644 --- a/Scripts/CartParameter.js +++ b/Scripts/CartParameter.js @@ -7,55 +7,20 @@ */ // 修改正则表达式,捕获新的请求 URL -const patterns = [ - // XboxWeb 老路径 - /^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; -} +const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/cart\/loadCart\?/; +const url = $request.url; // 判断是否是 PUT 请求并且 URL 匹配 -if (($request.method || "").toUpperCase() === "PUT" && matched) { +if ($request.method === "PUT" && pattern.test(url)) { try { - console.log(`Matched cart request: ${url}`); - // 从请求头中获取所需参数 - const xAuthorizationMuid = getHeader('x-authorization-muid'); - const msCv = getHeader('ms-cv'); - const xMsVectorId = getHeader('x-ms-vector-id'); - const xMsReferenceId = getHeader('x-ms-reference-id'); + const xAuthorizationMuid = $request.headers['x-authorization-muid']; + const msCv = $request.headers['ms-cv']; + const xMsVectorId = $request.headers['x-ms-vector-id']; + const xMsReferenceId = $request.headers['x-ms-reference-id']; // 新增参数获取 - const xMsTrackingId = getHeader('x-ms-tracking-id'); - const xMsCorrelationId = getHeader('x-ms-correlation-id'); - const xValidationField1 = getHeader('x-validation-field-1'); + const xMsTrackingId = $request.headers['x-ms-tracking-id']; + const xMsCorrelationId = $request.headers['x-ms-correlation-id']; // 更新存储逻辑(仅在新值有效时更新) let hasUpdate = false; @@ -88,27 +53,20 @@ if (($request.method || "").toUpperCase() === "PUT" && matched) { hasUpdate = true; } - // 5. x-ms-tracking-id + // 5. [新增] x-ms-tracking-id if (xMsTrackingId && xMsTrackingId !== $persistentStore.read("cart-x-ms-tracking-id")) { $persistentStore.write(xMsTrackingId, "cart-x-ms-tracking-id"); console.log(`Stored x-ms-tracking-id: ${xMsTrackingId}`); hasUpdate = true; } - // 6. x-ms-correlation-id + // 6. [新增] x-ms-correlation-id if (xMsCorrelationId && xMsCorrelationId !== $persistentStore.read("cart-x-ms-correlation-id")) { $persistentStore.write(xMsCorrelationId, "cart-x-ms-correlation-id"); console.log(`Stored x-ms-correlation-id: ${xMsCorrelationId}`); 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) { // $notification.post( @@ -120,7 +78,12 @@ if (($request.method || "").toUpperCase() === "PUT" && matched) { } catch (error) { console.log(`Error capturing parameters: ${error}`); + // $notification.post( + // "Surge 脚本错误", + // "无法捕获所需购物车参数", + // `${error}` + // ); } } -$done({}); \ No newline at end of file +$done({});