var k=-1
var j=-1
var data={}
var data_h={}
var finish=false
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
j++;
console.log('BeforeSendHeaders',j)
console.log(finish)
if(finish==false){
//{ console.log('head')
an=details.requestHeaders[1].value
data_h[j]=an
console.log(data_h)
}
}
, {
urls: ["https://mms.pinduoduo.com/sydney/api/goodsDataShow/queryGoodsSpanDateList"]
},
["blocking", "requestHeaders", "extraHeaders"]
)
chrome.webRequest.onBeforeRequest.addListener(
function(details)
{
k++;
console.log('beforerequest',k)
if(finish==false)
{
buffer=details.requestBody.raw[0].bytes
console.log(buffer)
var blob = new Blob([buffer]);
//将 Blob对象 读成字符串
var reader = new FileReader();
reader.onloadend = function (e) {
setTimeout(function(){ var send=reader.result; //a Hello world!
console.log(k,"次")
data[k]=send}, 1);
}
reader.readAsText(blob, 'utf-8');
}
//}
},
{urls: ["https://mms.pinduoduo.com/sydney/api/goodsDataShow/queryGoodsSpanDateList"]},
['requestBody']
);
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var j = 0; j < details.requestHeaders.length; ++j) {
if (details.requestHeaders[j].name === 'Origin')
details.requestHeaders[j].value = 'https://mms.pinduoduo.com';
}
}, {
urls: ["https://mms.pinduoduo.com/sydney/api/goodsDataShow/queryGoodsSpanDateList"]
},
["blocking", "requestHeaders", "extraHeaders"]
)
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
finish=request
sendResponse({head:data_h,body:data});
});
///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// //////// 此代码获取请求正文和请求标头,并将它们发送到content.js。 控制台从1开始打印'k',但不从0开始打印。我不知道为什么第一次filereader.onload不会第一次执行。 我试过console.log(buffer)可以正确打印,但是console.log(k,“次”)不能。我想这仅仅是因为reader.onload