From 04c4afe375293f35c10f3744c8e8bcc6128e1e88 Mon Sep 17 00:00:00 2001 From: XXhaos Date: Tue, 2 Jun 2026 00:06:53 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Scripts/CartParameter.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scripts/CartParameter.js | 71 ++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/Scripts/CartParameter.js b/Scripts/CartParameter.js index f5ee7a1..fe1b25a 100644 --- a/Scripts/CartParameter.js +++ b/Scripts/CartParameter.js @@ -7,20 +7,55 @@ */ // 修改正则表达式,捕获新的请求 URL -const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/cart\/loadCart\?/; -const url = $request.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; +} // 判断是否是 PUT 请求并且 URL 匹配 -if ($request.method === "PUT" && pattern.test(url)) { +if (($request.method || "").toUpperCase() === "PUT" && matched) { try { + console.log(`Matched cart request: ${url}`); + // 从请求头中获取所需参数 - 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 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 xMsTrackingId = $request.headers['x-ms-tracking-id']; - const xMsCorrelationId = $request.headers['x-ms-correlation-id']; + const xMsTrackingId = getHeader('x-ms-tracking-id'); + const xMsCorrelationId = getHeader('x-ms-correlation-id'); + const xValidationField1 = getHeader('x-validation-field-1'); // 更新存储逻辑(仅在新值有效时更新) let hasUpdate = false; @@ -53,20 +88,27 @@ if ($request.method === "PUT" && pattern.test(url)) { 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( @@ -78,12 +120,7 @@ if ($request.method === "PUT" && pattern.test(url)) { } catch (error) { console.log(`Error capturing parameters: ${error}`); - // $notification.post( - // "Surge 脚本错误", - // "无法捕获所需购物车参数", - // `${error}` - // ); } } -$done({}); +$done({}); \ No newline at end of file