Chrome 扩展 - 如何获取 HTTP 响应正文? [英] Chrome Extension - How to get HTTP Response Body?
问题描述
这似乎是一个难题(或不可能??).我想在观看 Chrome 扩展程序后台脚本的情况下获取并阅读由浏览器中的 HTTP 请求引起的 HTTP 响应.我们可以通过这种方式获取HTTP Request Body
It seems to be difficult problem (or impossible??). I want to get and read HTTP Response, caused by HTTP Request in browser, under watching Chrome Extension background script. We can get HTTP Request Body in this way
chrome.webRequest.onBeforeRequest.addListener(function(data){
// data contains request_body
},{'urls':[]},['requestBody']);
我也检查了这些stackoverflows
I also checked these stackoverflows
有没有什么聪明的方法可以在 Chrome 扩展中获取 HTTP 响应正文?
Is there any clever way to get HTTP Response Body in Chrome Extension?
推荐答案
我找不到比这个 anwser 更好的方法了.
I can't find better way then this anwser.
答案告诉了如何获取响应标题并显示在另一个页面中.但是响应对象中没有正文信息(请参阅event-responseReceived).如果您想在没有其他页面的情况下获得响应 body,请尝试此操作.
The answer told how to get response headers and display in another page.But there is no body info in the response obj(see event-responseReceived). If you want to get response body without another page, try this.
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(allEventHandler);
}
function allEventHandler(debuggeeId, message, params) {
if (currentTab.id != debuggeeId.tabId) {
return;
}
if (message == "Network.responseReceived") { //response return
chrome.debugger.sendCommand({
tabId: debuggeeId.tabId
}, "Network.getResponseBody", {
"requestId": params.requestId
}, function(response) {
// you get the response body here!
// you can close the debugger tips by:
chrome.debugger.detach(debuggeeId);
});
}
}
我认为它对我来说已经足够有用了,您可以使用 chrome.debugger.detach(debuggeeId)
来关闭丑陋的提示.
I think it's useful enough for me and you can use chrome.debugger.detach(debuggeeId)
to close the ugly tip.
抱歉,可能没有帮助... ^ ^
sorry, mabye not helpful... ^ ^
这篇关于Chrome 扩展 - 如何获取 HTTP 响应正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!