2

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?

Passerby
  • 9,715
  • 2
  • 33
  • 50
  • 1
    This is indeed a bug. Four months ago, I [also encountered this issue](http://stackoverflow.com/a/10339902/938089). Report the bug at http://new.crbug.com/ if you want it to be fixed (I wasn't able to find a duplicate). – Rob W Aug 14 '12 at 08:57
  • @RobW Thanks. While that's very inconvenient, it's still debug-able. But what about the second problem (pageAction icon disappears)? – Passerby Aug 14 '12 at 09:51
  • Does `obj.tabId` hold an expected value (not `-1`?). Throw in some `console.log`s, and watch the background page. – Rob W Aug 14 '12 at 09:58
  • @RobW Yes the `obj.tabId` does hold expected value. I `console.log(obj.tabId+": "+JSON.stringify(resHeaders))` several times before posting this question. – Passerby Aug 14 '12 at 10:08
  • The WebRequest API is activated before a page is actually rendered (important in the case of `main_frame`). It might be possible that the page unloads after the `webRequest` API has finished. When a page unloads, the pageAction icon is removed. If this is true, using `setTimeout(function(){chrome.pageAction.show(obj.tabId);}, 1000);` would show the page action about 1 second after the headers were received (no guarantee about the response body!). Rather than hacking around with delays, you can bind a `chrome.tabs.onUpdated` event. – Rob W Aug 14 '12 at 10:19
  • @RobW I tested this on my localhost where I hand make all pages......Anyway, `chrome.tabs.onUpdated` does seem to be able to work around this problem (caches an `var showIconTabId={}` in `background.js` and use `tabs.onUpdated` to refresh it). I'll try that. Many thanks. – Passerby Aug 14 '12 at 10:29

0 Answers0