首页 > 其他 > 详细

Chrome扩展如何获取响应体

时间:2020-06-11 20:01:57      阅读:272      评论:0      收藏:0      [点我收藏+]

将下面这个函数放到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

Chrome扩展如何获取响应体

原文:https://www.cnblogs.com/Higurashi-kagome/p/13095550.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!