Chrome extension: webRequest.onBeforeSendHeaders behaves strange

时光毁灭记忆、已成空白 提交于 2019-12-20 10:47:30

问题


I am trying to add a "Referer"-HTTP-Header to certain AJAX requests in my Chrome extension. You can't change it directly in the AJAX request so I tried to change it using the webRequest api:

chrome.webRequest.onBeforeSendHeaders.addListener(function(data) {
    console.log("onBeforeSendHeaders fired");
    var xdata=data.requestHeaders;
    xdata.push({
        "name":"Referer",
        "value": "http://the.new/referrer"
    })
    return {requestHeaders: xdata};
}, { //Filter
    urls: ["<all_urls>"], //For testing purposes
    types: ["xmlhttprequest"]
},["requestHeaders","blocking"]);

But this doesn't work for the AJAX requests in my extension. It only fires the event on other AJAX requests but not the ones done in my extension.
Another strange thing is that everything works fine when "blocking" flag is not set, but then I can't change the headers.

Does anyone know a way to solve this (or another way to achieve my goal: changing the "Referer" for a site request and retrieving the contents)

Thank you :)


回答1:


The reason you can't set the Referrer header when you don't have a blocking request is that the request has potentially already gone out - you are being notified asynchronously, and cannot change anything about the request.

To change headers, I use this code:

function mod_headers(header_array,p_name,p_value) {
     var did_set = false;                                                                                      
     for(var i in header_array) {                                                                                                   
         var header = header_array[i];                                                                                              
         var name = header.name;                                                                                                    
         var value = header.value;                                                                                                  

         // If the header is already present, change it:
         if(name == p_name) {
             header.value = p_value;
             did_set = true;
         }                                                                                                         
     }
     // if it is not, add it:
     if(!did_set) { header_array.push( { name : p_name , value : p_value } ); }                                                                                                                       
 }



回答2:


Here's a working version that works in all instances (when referer header is already set or when it hasn't been set.)

var requestFilter = {
    urls: ["<all_urls>"]
},

extraInfoSpec = ['requestHeaders', 'blocking'],
handler = function(details) {

var isRefererSet = false;
var headers = details.requestHeaders,
    blockingResponse = {};

for (var i = 0, l = headers.length; i < l; ++i) {
    if (headers[i].name == 'Referer') {
        headers[i].value = "http://your-url.com/";
        isRefererSet = true;
        break;
    }
}

if (!isRefererSet) {
    headers.push({
        name: "Referer",
        value: "http://your-url.com/"
    });
}

blockingResponse.requestHeaders = headers;
return blockingResponse;
};

chrome.webRequest.onBeforeSendHeaders.addListener(handler, requestFilter, extraInfoSpec);

Don't forget to add all the following permissions to your manifest:

"permissions": [  "webRequest", "webRequestBlocking", "<all_urls>" ]



回答3:


It's not working. See details at http://code.google.com/p/chromium/issues/detail?id=132731




回答4:


chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
     console.log(details);
},{urls: ["<all_urls>"]});


来源:https://stackoverflow.com/questions/10986077/chrome-extension-webrequest-onbeforesendheaders-behaves-strange

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