Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

javascript - Modify headers on onHeadersReceived

In my chrome extension I need to add a line to header of every site browsed. In background.js file I add such code:

var responseListener = function(details){
    var rule = {
        "name": "Access-Control-Allow-Origin",
        "value": "*"
    };
    details.responseHeaders.push(rule);
    return {responseHeaders: details.responseHeaders};
};

 chrome.webRequest.onHeadersReceived.addListener(responseListener,
     {urls: [   "*://*/*" ] },
     ["blocking", "responseHeaders"]);

While debugging the handler is called and newly added header successfully passes any filters I have found upper in the stack. But it is not seen on network tab's Response headers section and does not effects any code. I use these permissions:

"tabs","<all_urls>", "http://*/*" ,"webRequest","webRequestBlocking", "webNavigation"

Is there a new policy or API changed which disallow to do such things or there is some bug in my 10 lines of a code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Network tab of the Chrome Developer tools does not show the modifications from extensions. See https://crbug.com/258064

If you wish to see whether your extension has successfully modified a request, visit chrome://net-internals/#events, click on a request of type URL_REQUEST and look for URL_REQUEST_DELEGATE entries, e.g. URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED (this is an example of a log entry generated via the chrome.declarativeWebRequest API) or "delegate_info = "extension [extension name]" (generated by chrome.webRequest).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...