Chrome扩展程序 - 如何获取HTTP响应正文? [英] Chrome Extension - How to get HTTP Response Body?

查看:2775
本文介绍了Chrome扩展程序 - 如何获取HTTP响应正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是个难题(或不可能)。
我想通过浏览器中的HTTP请求获取并读取由HTTP扩展后台脚本引起的HTTP响应。
我们可以通过这种方式获得HTTP Request Body

  chrome.webRequest.onBeforeRequest.addListener(function(data){ 
// data contains request_body
},{'urls':[]},['requestBody']);

我也检查了这些计算器



是否有任何聪明的方法可以获取HTTP Chrome扩展中的响应正文?

解决方案

我无法找到更好的方式,然后这个anwser。

Chrome扩展程序可以读取HTTP响应



anwser告诉如何获取响应标题并显示在另一个页面中。但是响应中没有主体信息obj(请参阅 event-responseReceived )。如果您想在没有其他页面的情况下获得 body 响应,请尝试此操作。

  var currentTab; 
var version =1.0;

chrome.tabs.query(//获得当前标签
{
currentWindow:true,
active:true
},
function (tabArray){
currentTab = tabArray [0];
chrome.debugger.attach({//在当前标签处调试
tabId:currentTab.id
},version,onAttach .bind(null,currentTab.id));
}



function onAttach(tabId){

chrome.debugger .sendCommand({//首先启用Network
tabId:tabId
},Network.enable);

chrome.debugger.onEvent.addListener(allEventHandler);



$ b函数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){
//你在这里得到响应体!
//您可以通过以下方式关闭调试器提示:
chrome.debugger.detach(debuggeeId);
});
}

}

我认为这对我很有用您可以使用 chrome.debugger.detach(debuggeeId)关闭丑陋的提示。



对不起,mabye不是有帮助... ^ ^


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']);

I also checked these stackoverflows

Is there any clever way to get HTTP Response Body in Chrome Extension?

解决方案

I can't find better way then this anwser.

Chrome extension to read HTTP response

The anwser 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扩展程序 - 如何获取HTTP响应正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆