我有这个Chrome扩展程序,可以在发送请求之前修改请求标头。我现在希望能够在同一扩展中检查响应的标题。我搜索了整个Chrome扩展程序API,但我找不到任何有趣的内容。
这是我用来修改请求标头的代码,也许对你来说知道我是怎么做的很有用。
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {/*do something*/},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);
有谁知道怎么做,或者能指出一个有趣的来源?感谢
答案 0 :(得分:12)
通过向DOM注入脚本,我实现了捕获网站所做的所有HTTP请求和响应。我使用以下脚本将inject.js注入DOM:
/**
* code in inject.js
* added "web_accessible_resources": ["injected.js"] to manifest.json
*/
var s = document.createElement('script');
s.src = chrome.extension.getURL('injected.js');
s.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(s);
这会在与#34; content_scripts&#34;匹配的网站中注入inject.js; &#34;匹配&#34;在manifest.json中。在&#34; js&#34;中提及contentscript.js和inject.js。 此外,请确保您已在&#34;权限&#34;中提及了该网站。在manifest.json中。请参阅答案末尾的manifest.json。
现在,inject.js中实际捕获请求和响应的代码受到How we captured AJAX requests from a website tab with a Chrome Extension的启发。另请参阅该文章中的评论部分。
inject.js如下:
(function(xhr) {
var XHR = XMLHttpRequest.prototype;
var open = XHR.open;
var send = XHR.send;
var setRequestHeader = XHR.setRequestHeader;
XHR.open = function(method, url) {
this._method = method;
this._url = url;
this._requestHeaders = {};
this._startTime = (new Date()).toISOString();
return open.apply(this, arguments);
};
XHR.setRequestHeader = function(header, value) {
this._requestHeaders[header] = value;
return setRequestHeader.apply(this, arguments);
};
XHR.send = function(postData) {
this.addEventListener('load', function() {
var endTime = (new Date()).toISOString();
var myUrl = this._url ? this._url.toLowerCase() : this._url;
if(myUrl) {
if (postData) {
if (typeof postData === 'string') {
try {
// here you get the REQUEST HEADERS, in JSON format, so you can also use JSON.parse
this._requestHeaders = postData;
} catch(err) {
console.log('Request Header JSON decode failed, transfer_encoding field could be base64');
console.log(err);
}
} else if (typeof postData === 'object' || typeof postData === 'array' || typeof postData === 'number' || typeof postData === 'boolean') {
// do something if you need
}
}
// here you get the RESPONSE HEADERS
var responseHeaders = this.getAllResponseHeaders();
if ( this.responseType != 'blob' && this.responseText) {
// responseText is string or null
try {
// here you get RESPONSE TEXT (BODY), in JSON format, so you can use JSON.parse
var arr = this.responseText;
// printing url, request headers, response headers, response body, to console
console.log(this._url);
console.log(JSON.parse(this._requestHeaders));
console.log(responseHeaders);
console.log(JSON.parse(arr));
} catch(err) {
console.log("Error in responseType try catch");
console.log(err);
}
}
}
});
return send.apply(this, arguments);
};
})(XMLHttpRequest);
作为参考,我的manifest.json是:
{
"manifest_version": 2,
"name": "Extension Name",
"description": "Some Desc.",
"version": "1.1",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"storage",
"tabs",
"*://website.com/*"
],
"content_scripts": [
{
"matches": ["*://website.com/*"],
"run_at": "document_start",
"js": ["contentscript.js", "inject.js"]
}
],
"web_accessible_resources": ["injected.js"]
}
希望这有帮助。
答案 1 :(得分:2)
答案 2 :(得分:-1)
仅供参考,现在有了一种构建开发人员工具扩展的方法,该方法比上述方法更容易。 有关更多信息:https://developer.chrome.com/extensions/devtools_network