Firefox插件观察员http-on-modify-request不能正常工作 [英] Firefox Addon observer http-on-modify-request not working properly

查看:164
本文介绍了Firefox插件观察员http-on-modify-request不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的插件中有一个奇怪的错误,
插件本身需要为特定的域添加一个请求头参数,
这一切都正常,但是错误是,那观察者http-on-modify-request在启动时不会被调用,只有当我重新加载页面的时候,它才能正常工作。

我的意思是:


  1. 我转到mysite.com/ - 未修改标题
  2. 重新加载 - 标题模式

  3. mysite.com/中的新选项卡 - 未修改标头
  4. / li>
  5. 重新加载选项卡 - 标题模式化

我使用addon sdk: / b>

  exports.main = function(options,callbacks){

//创建观察者
httpRequestObserver =
{
观察:函数(主题,主题,数据)
{

if(topic ==http-on-modify-request){


//只能识别特定的偏好域
var windowsService = Cc ['@ mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
var uri = windowsService.getMostRecentWindow('navigator:browser')。getBrowser()。currentURI;
var domainloc = uri.host;

if(domainloc ==mysite.com){
var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
httpChannel.setRequestHeader(x-test,test,false);





$ b register:function()
{
var observerService = Cc [@ mozilla.org / observer-service; 1]
.getService(Ci.nsIObserverService);
observerService.addObserver(this,http-on-modify-request,false);


unregister:function()
{
var observerService = Cc [@ mozilla.org/observer-service;1]
.getService(Ci.nsIObserverService);
observerService.removeObserver(这个http-on-modify-request);


}
};


//注册观察者
httpRequestObserver.register();

};

exports.onUnload = function(reason){

httpRequestObserver.unregister();
};

请帮助我,我搜索了几个小时没有结果。
代码正在工作,但不是在第一次页面加载,
只有当我重新加载。



目标是只有mysite。 com,那么一直会有 x-text = test 标题请求,但是只能在mysite.com上。

是当前在选项卡中加载的Uri。在http-on-modify-request通知时间,请求不会发送到服务器,所以如果它是一个新的选项卡,浏览器没有任何 currentUri 。当你刷新选项卡时,它使用当前页面的URI,它看起来像。



试试这个: p>

  if(topic ==http-on-modify-request){
var httpChannel = subject.QueryInterface(Ci .nsIHttpChannel);
var uri = httpChannel.URI;
var domainloc = uri.host;

//只识别特定的首选项域
if(domainloc ==mysite.com){
httpChannel.setRequestHeader(x-test,test,假);
}
}


I have a weird bug in my addon, the addon itself needs to add a request header parameters for a specific domain, it's all working, but the bug is, that the observer http-on-modify-request is not called at start, only if I reload the page, then it's working.

I mean:

  1. I go to mysite.com/ - no header modified,
  2. I reload page - header modefied
  3. reload again - header modefied
  4. new tab at mysite.com/ - no header modified
  5. reload tab - header modefied

My code, I'm using the addon sdk:

exports.main = function(options,callbacks) {

// Create observer 
httpRequestObserver =  
{  
  observe: function(subject, topic, data)  
  {  

    if (topic == "http-on-modify-request") {


    //only identify to specific preference domain
    var windowsService = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
    var uri = windowsService.getMostRecentWindow('navigator:browser').getBrowser().currentURI;
    var domainloc = uri.host;

        if (domainloc=="mysite.com"){
            var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);  
            httpChannel.setRequestHeader("x-test", "test", false);  
        }
    }  


  }, 

  register: function()  
  { 
    var observerService = Cc["@mozilla.org/observer-service;1"]  
            .getService(Ci.nsIObserverService);  
    observerService.addObserver(this, "http-on-modify-request", false);         
  },  

  unregister: function()  
  {
    var observerService = Cc["@mozilla.org/observer-service;1"]  
            .getService(Ci.nsIObserverService);  
    observerService.removeObserver(this, "http-on-modify-request"); 


  }  
};


//register observer
httpRequestObserver.register();

};

exports.onUnload = function(reason) {

httpRequestObserver.unregister();
};

Please help me, I searched for hours with no results. The code is working, but not at first time of page loading, only if I reload.

The goal is that only on mysite.com, there will be a x-text=test header request, all the time, but only on mysite.com.

解决方案

currentUri on browser is the Uri that is currently loaded in the tab. At "http-on-modify-request" notification time, the request is not sent to the server yet, so if it's a new tab, the browser doesn't have any currentUri. When you refresh the tab in place, it uses the uri of the current page, and it seemingly works.

Try this instead:

if (topic == "http-on-modify-request") {
    var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);  
    var uri = httpChannel.URI;
    var domainloc = uri.host;

    //only identify to specific preference domain
    if (domainloc == "mysite.com") {
        httpChannel.setRequestHeader("x-test", "test", false);
    }
}

这篇关于Firefox插件观察员http-on-modify-request不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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