将下面这个函数放到background.js中。
让popup.js调用函数就可以将响应体打印到背景页的控制台上。
function getResponse(){
var currentTab;
var version = "1.0";
chrome.tabs.query( //get current Tab
{
currentWindow: true,
active: true
},
function(tabArray) {
currentTab = tabArray[0];
chrome.debugger.attach({ //debug at current tab
tabId: currentTab.id
}, version, onAttach.bind(null, currentTab.id));
}
)
function onAttach(tabId) {
chrome.debugger.sendCommand({ //first enable the Network
tabId: tabId
}, "Network.enable");
chrome.debugger.onEvent.addListener(function(debuggeeId, message, params){
if (currentTab.id != debuggeeId.tabId) {
alert("currentTab.id != debuggeeId.tabId")
return;
}
if (message == "Network.responseReceived") {
chrome.debugger.sendCommand({
tabId: debuggeeId.tabId
}, "Network.getResponseBody", {
"requestId": params.requestId
}, function(response) {
console.log("****************")
console.log(JSON.stringify(response))
});
}
});
}
}
参考:chrome-extension-how-to-get-http-response-body
原文:https://www.cnblogs.com/Higurashi-kagome/p/13095550.html