Chrome Extension - How to Get Http Response Body

Chrome Extension - How to get HTTP Response Body?

I can't find better way then this anwser.

Chrome extension to read HTTP response

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);
});
}

}

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 extension to read HTTP response

See the live-headers example.

http://code.google.com/chrome/extensions/examples/api/debugger/live-headers.zip

EDIT: For posterity you can find a version of live-headers.zip on their archived bug/patch site https://chromiumcodereview.appspot.com/9289057

With the latest revision (2021) no longer including the zip, but here's the dir https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/debugger/live-headers/?pathrev=226223

Modify HTTP responses from a Chrome extension

In general, you cannot change the response body of a HTTP request using the standard Chrome extension APIs.

This feature is being requested at 104058: WebRequest API: allow extension to edit response body. Star the issue to get notified of updates.

If you want to edit the response body for a known XMLHttpRequest, inject code via a content script to override the default XMLHttpRequest constructor with a custom (full-featured) one that rewrites the response before triggering the real event. Make sure that your XMLHttpRequest object is fully compliant with Chrome's built-in XMLHttpRequest object, or AJAX-heavy sites will break.

In other cases, you can use the chrome.webRequest or chrome.declarativeWebRequest APIs to redirect the request to a data:-URI. Unlike the XHR-approach, you won't get the original contents of the request. Actually, the request will never hit the server because redirection can only be done before the actual request is sent. And if you redirect a main_frame request, the user will see the data:-URI instead of the requested URL.



Related Topics



Leave a reply



Submit