我可以将内容长度标头注入Chrome吗? [英] Can I inject a Content-Length header into Chrome?
问题描述
我正在尝试制作我的第一个Chrome扩展程序,这已经是一次颇具学习的体验。我几乎完成了,但这最后一块是杀了我。
我知道谷歌不希望你使用Content-Length(如这里),但我正在使用身份验证的API在POST时需要它。 此扩展说它可以做到,但我觉得我写的东西应该可以工作。这是最近在Chrome的新版本中被封锁的吗?
这些是我发送的头文件:
{
ReplaceHeaders:true,
rh-Authentication:< my auth token>,
rh-Timestamp:< ;时间戳>,
rh-Content-Length:body.length
}
这是将新的标题替换为rh标题的部分(除了rh-之外,所有内容都是rh-),如果这样做更合理的话。):
chrome.webRequest.onBeforeSendHeaders.addListener(function(data){
newHeaders = false;
_.find(data (header.name ===ReplaceHeaders)&&(header.value)){
_.find(data.requestHeaders,function( h){
if(h.name.substring(0,3)===rh-){
if(newHeaders === false){newHeaders = [];}
newHeaders.push({
name:h.name.substring(3,h.name.length),
value:h.value
});
}
});
return;
}
});
if(!! newHeaders){
//更新标题
return {requestHeaders:newHeaders};
}
},{urls:[< all_urls>]},[requestHeaders,blocking]);
当我发出GET请求时,我没有rh-ContentLength标题,它起作用没问题(我可以看到SendHeaders,onHeadersReceived等)。一切都被替换,并且响应如预期。但是,当我使用rh-Content-Length发出POST请求时,我只能看到onSendHeaders,之后什么也没有。
我的onSendHeaders对于GET和POST都有新格式标题。
好的,所以我在凌晨3点发布。在得到一些急需的睡眠后,我今天早上醒来,并与一位朋友谈论此事。他指出,这个错误实际上是在我的ajax调用中。我解决了这个问题,现在我可以从我的扩展中发送Content-Length。上面的代码确实有效。
关于我之前发布的代码的一些注记,以供任何人稍后搜索: 尽管未经过测试,在其他标题,Chrome说它不会通过。以下是谷歌表示它在onBeforeSendHeaders中没有提供的头文件列表(如 here ): 希望这有助于您。 I'm trying to make my first Chrome Extension and it has been quite a learning experience. I'm almost done, but this last piece is killing me. I know Google doesn't want you to use Content-Length (as stated here), but I'm working with an API who's authentication requires it when I POST. This extension says it can do it but I feel like what I wrote should work. Was this recently blocked in a new version of Chrome? These are the headers I am sending: This is the section that replaces the "rh" headers with the new headers (everything with "rh-", except for the "rh-" if that even makes sense.): When I make a GET request I don't have the "rh-ContentLength" header and it works fine (I can see onSendHeaders, onHeadersReceived, etc.). Everything is replaced and the response is as expected. But when I make a POST request with "rh-Content-Length" I only see onSendHeaders and nothing after that. My onSendHeaders for both GET and POST have the newly formatted headers. Ok, so I posted that at 3:00 AM. After getting some much needed sleep, I woke up this morning and talked to a friend about it. He pointed out that the mistake was actually in my ajax call. I fixed that and now I can send Content-Length from my extension. The above code does work. A few notes for the code I posted above for anybody searching for this later: Although not tested, this should work on other headers that Chrome says it won't pass. Here is a list of the headers Google says it doesn't provide in onBeforeSendHeaders (as documented here): Hope this helps. 这篇关于我可以将内容长度标头注入Chrome吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
$ b
{
"ReplaceHeaders": true,
"rh-Authentication": "<my auth token>",
"rh-Timestamp": "<timestamp>",
"rh-Content-Length": body.length
}
chrome.webRequest.onBeforeSendHeaders.addListener(function(data) {
newHeaders = false;
_.find(data.requestHeaders, function(header) {
if ((header.name === "ReplaceHeaders") && (header.value)) {
_.find(data.requestHeaders, function(h) {
if (h.name.substring(0, 3) === "rh-") {
if (newHeaders === false) { newHeaders = []; }
newHeaders.push({
name: h.name.substring(3, h.name.length),
value: h.value
});
}
});
return;
}
});
if (!!newHeaders) {
// Update headers
return {requestHeaders: newHeaders};
}
}, { urls: ["<all_urls>"] }, ["requestHeaders", "blocking"]);