Chrome extension API webRequest doesn't work well on response

妖精的绣舞 提交于 2019-12-22 12:31:28

问题


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?

来源:https://stackoverflow.com/questions/11945665/chrome-extension-api-webrequest-doesnt-work-well-on-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!