我正在开发一个Firefox附加组件来拦截HTTP请求并提取cookie。我能够从标题中提取“用户代理”但无法提取cookie。我使用的代码如下。
chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
var headers = details.requestHeaders,
blockingResponse = {};
for( var i = 0, l = headers.length; i < l; ++i ) {
window.alert("Checking headers");
if( headers[i].name == 'Cookie' ) {
headers[i].value = 'twid=notsecret';
window.alert("Cookie Changed");
console.log(headers[i].value);
break;
}
}
blockingResponse.requestHeaders = headers;
return blockingResponse;
},
{urls: [ "<all_urls>" ]},['requestHeaders','blocking']);
为什么这不起作用,是否有替代方法?
答案 0 :(得分:1)
您可能正在使用早于49.0a版本的Firefox版本进行测试:
最不可能的原因是您正在使用早于49.0a版本(当前处于测试阶段)的Firefox版本进行测试。如果是这样,当您尝试使用window.alert()
时,您的代码将抛出错误。通过Firefox版本49.0a,alert()
不再抛出错误,但打开Browser Console并打印以下行(除了您尝试在alert()
中显示的文本外):
alert() is not supported in background windows; please use console.log instead.
在49.0a之前的Firefox版本中,当您尝试alert()
时,您将在浏览器控制台中看到以下错误:
NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindowUtils.isParentWindowMainWidgetVisible] nsPrompter.js:346
uncaught exception: unknown (can't convert to string) (unknown)
在您的实例中,在您的webRequest侦听器中使用alert()
,错误消息会有所不同:
NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindowUtils.isParentWindowMainWidgetVisible] nsPrompter.js:346
[Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMWindowUtils.isParentWindowMainWidgetVisible]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: resource://gre/components/nsPrompter.js :: openModalWindow :: line 346" data: no] (unknown)
您的代码按照书面形式运作:
除此之外,您的代码按照问题编写。但是,当您通过console.log()
输出新的Cookie值时,不会验证请求标头实际上是否已更改。为此,您需要在next event to fire in the chain上添加一个额外的监听器:webRequest.onSendHeaders
。
以下是一些修改后的代码,用于验证Cookie标头是否已实际更改。它已经过测试和验证,可以在FF48.0.2 +中使用:
的manifest.json :
{
"description": "Demonstrate changing the cookie of a WebRequest",
"manifest_version": 2,
"name": "change-webrequest-cookie-demo",
"version": "0.1",
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>" //Required for Google Chrome. Not, currently, needed for Firefox.
],
"background": {
"scripts": ["background.js"]
}
}
background.json :
/*Demonstrate changing the cookie of a WebRequet */
// For testing, open the Browser Console
try{
//Alert is not supported in Firefox. In in FF49.0+, forces the Browser Console open.
alert('Open the Browser Console.');
}catch(e){
//alert throws an error in Firefox versions earlier than 49.0
console.log('Alert threw an error. Probably, the version of Firefox is less than 49.');
}
chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
var blockingResponse = {};
//console.log("Checking headers");
details.requestHeaders.some(function(header){
if( header.name == 'Cookie' ) {
console.log("Original Cookie value:" + header.value);
header.value = 'twid=notsecret';
console.log("Cookie Changed");
return true;
}
return false;
});
blockingResponse.requestHeaders = details.requestHeaders;
return blockingResponse;
}, {urls: [ "<all_urls>" ]},['requestHeaders','blocking']);
chrome.webRequest.onSendHeaders.addListener(function(details){
details.requestHeaders.some(function(header){
if( header.name == 'Cookie' ) {
console.log("New Cookie value:" + header.value);
return true;
}
return false;
});
}, {urls: [ "<all_urls>" ]},['requestHeaders']);
使用Firefox Developer Edition或Firefox Nightly:
WebExtensions API仍处于开发阶段。每个版本的Firefox都有所改进。目前,您最好使用Firefox Developer Edition或Firefox Nightly开发和测试您的WebExtension插件。您还应该仔细注意您希望使用的功能所需的Firefox版本。此信息包含在&#34;浏览器兼容性&#34; MDN文档页面的一部分。
使用Browser Console:
您应该使用Browser Console到view console.log()
output from you WebExtension background scripts。如果您一直在查看浏览器控制台,则会看到错误消息。您仍然需要弄清楚错误消息的含义。但是,您至少可以搜索更多内容并包含在您的问题中。