gpt4 book ai didi

javascript - Chrome 扩展 - 如何获取 HTTP 响应正文?

转载 作者:行者123 更新时间:2023-12-03 00:07:55 25 4
gpt4 key购买 nike

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

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

我还检查了这些 stackoverflow

有什么聪明的方法可以在 Chrome 扩展程序中获取 HTTP 响应正文吗?

最佳答案

我找不到比这个答案更好的方法了。

Chrome extension to read HTTP response

答案告诉我们如何获取响应 header 并显示在另一个页面中。但是响应对象中没有正文信息(请参阅 event-responseReceived )。如果您想在没有其他页面的情况下获得响应正文,请尝试此操作。

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)来关闭丑陋的提示。

抱歉,也许没有帮助... ^ ^

关于javascript - Chrome 扩展 - 如何获取 HTTP 响应正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18534771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com