【问题标题】:Firefox Addon observer http-on-modify-request not working properlyFirefox 插件观察者 http-on-modify-request 无法正常工作
【发布时间】:2014-02-08 23:11:00
【问题描述】:

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

我的意思是:

  1. 我去 mysite.com/ - 没有修改标题
  2. 我重新加载页面 - 标题已修改
  3. 重新加载 - 标头已修改
  4. mysite.com/ 上的新标签页 - 没有修改标题
  5. 重新加载选项卡 - 已修改标题

我的代码,我正在使用插件 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();
};

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

目标是只有在 mysite.com 上,才会一直有一个 x-text=test 标头请求,但只在 mysite.com 上。

【问题讨论】:

    标签: javascript firefox http-headers firefox-addon reload


    【解决方案1】:

    浏览器上的currentUri 是当前加载到选项卡中的 Uri。在“http-on-modify-request”通知时间,请求还没有发送到服务器,所以如果是新标签,浏览器没有currentUri。当您在适当位置刷新选项卡时,它使用当前页面的 uri,并且它似乎有效。

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2014-03-24
      • 1970-01-01
      • 2021-06-26
      相关资源
      最近更新 更多