126 lines
4.8 KiB
JavaScript
126 lines
4.8 KiB
JavaScript
/**
|
||
* Surge 脚本:捕获特定 URL 请求的参数
|
||
* 包含: x-authorization-muid, ms-cv, x-ms-vector-id, x-ms-reference-id
|
||
* 新增: x-ms-tracking-id, x-ms-correlation-id
|
||
* 并存储到持久化存储($persistentStore)中
|
||
* 只针对 PUT 请求
|
||
*/
|
||
|
||
// 修改正则表达式,捕获新的请求 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 || "").toUpperCase() === "PUT" && matched) {
|
||
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 xMsTrackingId = getHeader('x-ms-tracking-id');
|
||
const xMsCorrelationId = getHeader('x-ms-correlation-id');
|
||
const xValidationField1 = getHeader('x-validation-field-1');
|
||
|
||
// 更新存储逻辑(仅在新值有效时更新)
|
||
let hasUpdate = false;
|
||
|
||
// 1. x-authorization-muid
|
||
if (xAuthorizationMuid && xAuthorizationMuid !== $persistentStore.read("cart-x-authorization-muid")) {
|
||
$persistentStore.write(xAuthorizationMuid, "cart-x-authorization-muid");
|
||
console.log(`Stored x-authorization-muid: ${xAuthorizationMuid}`);
|
||
hasUpdate = true;
|
||
}
|
||
|
||
// 2. ms-cv
|
||
if (msCv && msCv !== $persistentStore.read("cart-ms-cv")) {
|
||
$persistentStore.write(msCv, "cart-ms-cv");
|
||
console.log(`Stored ms-cv: ${msCv}`);
|
||
hasUpdate = true;
|
||
}
|
||
|
||
// 3. x-ms-vector-id
|
||
if (xMsVectorId && xMsVectorId !== $persistentStore.read("cart-x-ms-vector-id")) {
|
||
$persistentStore.write(xMsVectorId, "cart-x-ms-vector-id");
|
||
console.log(`Stored x-ms-vector-id: ${xMsVectorId}`);
|
||
hasUpdate = true;
|
||
}
|
||
|
||
// 4. x-ms-reference-id
|
||
if (xMsReferenceId && xMsReferenceId !== $persistentStore.read("cart-x-ms-reference-id")) {
|
||
$persistentStore.write(xMsReferenceId, "cart-x-ms-reference-id");
|
||
console.log(`Stored x-ms-reference-id: ${xMsReferenceId}`);
|
||
hasUpdate = true;
|
||
}
|
||
|
||
// 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
|
||
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(
|
||
// "Surge 信息存储",
|
||
// "已捕获并存储所有加购请求参数",
|
||
// "包含 tracking-id 和 correlation-id"
|
||
// );
|
||
// }
|
||
|
||
} catch (error) {
|
||
console.log(`Error capturing parameters: ${error}`);
|
||
}
|
||
}
|
||
|
||
$done({}); |