Chrome扩展程序:webRequest.onBeforeSendHeaders行为很奇怪 [英] Chrome extension: webRequest.onBeforeSendHeaders behaves strange

查看:1706
本文介绍了Chrome扩展程序:webRequest.onBeforeSendHeaders行为很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Chrome扩展程序中为某些AJAX请求添加Referer-HTTP-Header。
您无法直接在AJAX请求中更改它,所以我尝试使用 webRequest api

  chrome.webRequest.onBeforeSendHeaders.addListener(函数(数据){
console.log(onBeforeSendHeaders fired);
var xdata = data.requestHeaders;
xdata.push({
name:Referer ,
value:http://the.new/referrer
})
return {requestHeaders:xdata};
},{//过滤
urls:[< all_urls>],//用于测试目的
类型:[xmlhttprequest]
},[requestHeaders,blocking]);

但这不适用于我的扩展程序中的AJAX请求。它只会触发其他AJAX请求上的事件,但不会在我的扩展中完成的事件。

另一个奇怪的事情是,没有设置阻止标志时,一切正常,但后来我无法更改标题。



有没有人知道解决这个问题的方法(或其他方式来实现我的目标:更改站点请求的Referer并检索内容) p>

谢谢:)

解决方案

您无法设置的原因如果您没有阻止请求,推荐人标题是请求可能已经过期 - 您将被异步通知,并且无法更改有关请求的任何内容。



要改变头文件,我使用下面的代码:

pre $ function $ mod_headers假;
for(var i in header_array){
var header = header_array [i];
var name = header.name;
var value = header.value;

//如果头文件已经存在,改变它:
if(name == p_name){
header.value = p_value;
did_set = true;


//如果不是,添加它:
if(!did_set){header_array.push({name:p_name,value:p_value}); }
}


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 :)

解决方案

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 } ); }                                                                                                                       
 }

这篇关于Chrome扩展程序:webRequest.onBeforeSendHeaders行为很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆