onHeadersReceived unable to modify response headers

2,961 views
Skip to first unread message

TomM

unread,
Mar 6, 2012, 3:41:01 AM3/6/12
to Chromium-extensions
Hi

I am trying to modify response headers of XMLHttpRequest made within
websites or content scripts. I have successfully modified request
headers and I thought response headers should follow in kind but
simply with a different injection point.

Modifying the response headers does not appear to be working in the
chrome debugger. Here is a cut down version of the code I am using, I
have stepped thru the code as well as console logging the values I am
returning, everything looks correct and yet the new headers are still
not reflected.

chrome.webRequest.onHeadersReceived.addListener(function(details){

details.responseHeaders[details.responseHeaders.length] = {name: 'X-
modified', value: 'done'};
return {responseHeaders: details.responseHeaders};

},{urls:["<all_urls>"],types:["xmlhttprequest","sub_frame"]},
["responseHeaders","blocking"]);


Does anyone with experience with onHeadersReceived know what the issue
might be. Thanks to any and all who respond.

Regards,
Tom

x29a

unread,
Mar 6, 2012, 3:55:37 AM3/6/12
to TomM, Chromium-extensions
Hi Tom,

which permissions did you request in your manifest? I found, that

"http://*/*"

was necessary for the response headers to be modified. Also, only one
extension can modify the response headers, so youll have to disable all
other "candidates".

Did this help? If not, can you post your manifest nevertheless?

bye

TomM

unread,
Mar 6, 2012, 8:01:49 AM3/6/12
to Chromium-extensions
Hey, Thanks for your reply.

As you suggested I added http://*/* even though I already had
<all_urls> just for testing sake and it still did not work. My
manifest is below.

{
"background_page": "background.html",
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["on.js"],
"run_at": "document_end"
}
],
"description": "Description Removed",
"icons": {
"128": "icon128.png",
"16": "icon16.png",
"48": "icon48.png"
},
"name": "Name Removed",
"permissions": [ "tabs","<all_urls>", "http://*/
*" ,"webRequest","webRequestBlocking" ],
"version": "1.0.0"
}

I have disabled all other plugins and restarted the browser numerous
times.

Tom

Mike West

unread,
Mar 6, 2012, 8:53:19 AM3/6/12
to Chromium-extensions, TomM
If you examine the headers coming back from the XMLHTTPRequest (via
the `.getAllResponseHeaders()` method), I think you'll see that your
header was correctly set. It's apparently not showing up in the Web
Inspector, however. I suspect that's getting populated before the
WebRequest APIs are triggered, which is a bit of a problem for
debugging. That's what I'm seeing with the following code, anyway:

chrome.webRequest.onHeadersReceived.addListener(
function(details) {
console.log(details);
details.responseHeaders.splice(13);
details.responseHeaders[details.responseHeaders.length] = {
name: 'X-AMAZING',
value: 'WOW'
};
return {
responseHeaders: details.responseHeaders
};
},
{
urls: ["<all_urls>"],
types: ["xmlhttprequest"]
},
['responseHeaders', 'blocking']);

Would you mind filing a bug at http://new.crbug.com/? I'll make sure
the devtools and webrequest developers see it.

Thanks!

-Mike

im007boy

unread,
Mar 31, 2012, 12:41:07 AM3/31/12
to Mike West, Chromium-extensions, TomM
This code is invalid in beta version(18.0.1025.142)


2012/3/6 Mike West <mk...@chromium.org>
--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Reply all
Reply to author
Forward
0 new messages