So I'm trying the Chrome API webRequest. Everything works fine on request, but on response, I got problems.
My testing is pretty straight forward:
function func(obj)
{
var resHeaders=obj.responseHeaders;
for(var i=0;i<resHeaders.length;i++)
{
if(resHeaders[i].name=="X-Powered-By" && resHeaders[i].value.indexOf("PHP")>=0)
{
resHeaders[i].value="Extension";
resHeaders.push({name:"X-Test",value:"Found"});
chrome.pageAction.show(obj.tabId);
break;
}
}
return {responseHeaders:resHeaders};
}
chrome.webRequest.onHeadersReceived.addListener(func,{urls:["<all_urls>"]},["blocking","responseHeaders"]);
To my surprise, Chrome Developer Tool always shows the original header even though the header is correctly modified (I have to use
XMLHttpRequest.getAllResponseHeaders()
to tell). This is very inconvenience because I have to made many XHR to debug instead of trying on real world webpages.
Edit: Confirmed by @RobW, this is a bug, so it belongs to crbug.com...
The bigger problem is, if the modification is not made on the last request that before the load event, the pageAction icon would not show up.
E.g. If a page contains four requests:
HTML -> triggers modification
CSS
JS
[Load event]
HTML inside
iframe
-> triggers modification
pageAction icon stays;
But if a page contains three requests:
HTML -> triggers modification
CSS
JS
[Load event]
pageAction icon would show up and disappear (I can see this on a slow XP machine; on fast machine it just doesn't show up).
But if I then hand made an XHR (triggers the modification), the pageAction icon correctly stays on address bar.
This problem is annoying because I have to let the user (if any) know that the extension is in effect.
Same thing happened on Win 7 and XP, latest Chrome (21+, stable). Is this expected, or am I doing something wrong?