xhr修改响应数据
(function () {
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
let urls = ['api/user']
XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
this._method = method;
this._url = url;
this._async = async;
originalOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
// 调用原始的 setRequestHeader 方法,而不是重写后的方法
originalSetRequestHeader.call(this, header, value);
};
XMLHttpRequest.prototype.send = function (body) {
const xhr = this;
// 监听 `readystatechange` 事件
xhr.addEventListener("readystatechange", function () {
if (xhr.readyState === 3 && urls.filter(u=>xhr._url.includes(u)).length>0) {
try {
// 拦截并修改响应数据
const originalResponse = xhr.responseText;
const jsonResponse = JSON.parse(originalResponse);
console.log("原始响应数据:", jsonResponse);
if (jsonResponse && jsonResponse.data && jsonResponse.data.adInfos) {
jsonResponse.data.adInfos.forEach((order) => {
order.modified = true;
if (order.bid) order.bid = `${Number(order.bid) * 1.2}`;
if (order.ecpRoiGoal) order.ecpRoiGoal = order.ecpRoiGoal * 0.8;
});
const modifiedResponse = JSON.stringify(jsonResponse);
console.log("修改后的响应数据:", jsonResponse);
// 重新定义 responseText,确保页面访问的是修改后的数据
Object.defineProperty(xhr, "responseText", {
get: function () {
return modifiedResponse;
},
});
}
} catch (error) {
console.error("解析或修改响应数据时出错:", error);
}
}
});
// 调用原始 send 方法
originalSend.apply(this, arguments);
};
})();
fetch
(function () {
const originalXMLHttpRequest = window.XMLHttpRequest;
function CustomXMLHttpRequest() {
this._method = null;
this._url = null;
this._headers = {};
this._body = null;
this.readyState = 0;
this.status = 0;
this.statusText = '';
this.responseText = '';
this.response = null;
// 模拟事件处理器
this.onreadystatechange = null;
}
// 模拟 XMLHttpRequest 的 open 方法
CustomXMLHttpRequest.prototype.open = function (method, url, async, user, password) {
this._method = method;
this._url = url;
this._async = async;
this.readyState = 1; // 状态变为 OPENED
if (this.onreadystatechange) {
this.onreadystatechange();
}
};
// 模拟 XMLHttpRequest 的 setRequestHeader 方法
CustomXMLHttpRequest.prototype.setRequestHeader = function (header, value) {
this._headers[header] = value;
};
// 模拟 XMLHttpRequest 的 send 方法
CustomXMLHttpRequest.prototype.send = async function (body) {
this._body = body;
const fetchOptions = {
method: this._method,
headers: this._headers,
body: ["GET", "HEAD"].includes(this._method.toUpperCase()) ? undefined : body, // 确保 GET/HEAD 无 body
};
try {
const response = await fetch(this._url, fetchOptions);
const clonedResponse = response.clone();
// 解析响应数据
let text = await clonedResponse.text();
// 修改响应数据(根据实际需求)
if (urls.filter(u=>this._url.includes(u)).length>0) {
const jsonResponse = JSON.parse(text);
console.log('修改获取列表之前', jsonResponse)
jsonResponse.data.adInfos.forEach((order) => {
order.modified = true;
if (order.bid) order.bid = `${Number(order.bid) * 1.2}`;
if (order.ecpRoiGoal) order.ecpRoiGoal = order.ecpRoiGoal * 1.2;
});
// jsonResponse.data.adInfos = []
text = JSON.stringify(jsonResponse);
console.log('修改获取列表之后', jsonResponse)
// text = 1111111
}
// 模拟响应
this.responseText = text;
this.response = text;
this.status = clonedResponse.status;
this.statusText = clonedResponse.statusText;
this.readyState = 4;
if (this.onreadystatechange) {
this.onreadystatechange();
}
} catch (error) {
console.error("Fetch 请求失败:", error);
this.readyState = 4;
this.status = 500;
this.statusText = 'Internal Server Error';
if (this.onreadystatechange) {
this.onreadystatechange();
}
}
};
// 替换原生 XMLHttpRequest
window.XMLHttpRequest = CustomXMLHttpRequest;
})();
let urls = ['api/user']
这个是chase网站的吗 可以加个微信吗老板
zhuli1321