如何为firefox编译此扩展?

时间:2012-08-30 18:28:50

标签: firefox build firefox-addon firefox-addon-sdk

如何将其编译成firefox插件?我曾尝试使用位于https://builder.addons.mozilla.org的firefox的插件构建器,但是当我点击测试时它说“XPI not built”。任何人都可以告诉我如何将此代码转换为函数firefox扩展?谢谢。

//create an nsIObserver implementor
  var listener = {
observe : function(aSubject, aTopic, aData) {
      var httpChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);


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

        var channel= aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);

        if(channel.requestMethod == "POST")
        {

            channel=channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
            channel = channel.uploadStream;  
            channel.QueryInterface(Components.interfaces.nsISeekableStream)
                            .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET,    0);  
            var stream = Components.classes["@mozilla.org/binaryinputstream;1"]
                                .createInstance(Components.interfaces.nsIBinaryInputStream);  
            stream.setInputStream(channel);  
            var postBytes = stream.readByteArray(stream.available());  
            poststr = String.fromCharCode.apply(null, postBytes);  

            //change the poststr

            // poststr=poststr.replace(....);  

            // ERROR HERE - stringStream is not defined
            stringStream.setData(poststr, poststr.length);  
            //changing the postdata  
            channel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
            channel = channel.uploadStream;  
            channel = channel.QueryInterface(Components.interfaces.nsISeekableStream)
                      .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
                channel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);  
            channel.uploadStream.setData(stringStream);  
            channel.requestMethod = "POST";

        }

      }

},

QueryInterface : function(aIID) {
   if (aIID.equals(Components.interfaces.nsISupports) ||
        aIID.equals(Components.interfaces.nsIObserver))
     return this;
   throw Components.results.NS_NOINTERFACE;
}
 };

 var observerService = null;

var Init = {
addObserver : function(){
  observerService = Components.classes["@mozilla.org/observer-    service;1"].getService(Components.interfaces.nsIObserverService);
  observerService.addObserver(listener, "http-on-modify-request", false);
},

removeObserver : function(){
  observerService.removeObserver(listener, "http-on-modify-request");
}
};

Init.addObserver();
  • 编辑:我已经阅读了官方的Firefox文档,但它很混乱,它主要处理基于gui的插件,并没有提供很多示例我将如何为此创建扩展。

1 个答案:

答案 0 :(得分:3)

您所拥有的示例是针对经典(基于XUL)的扩展,而不是针对附加SDK。您仍然可以使用low-level observer-service API添加该观察者。此外,基于SDK的扩展通常无法访问Components对象,它需要chrome authority。将以下代码添加到main.js文件中:

var observer = require("observer-service");
var {Cc, Ci} = require("chrome");

observer.add("http-on-modify-request", function(subject, data)
{
  var channel= subject.QueryInterface(Ci.nsIHttpChannel);

  if (channel.requestMethod == "POST")
  {
    channel = channel.QueryInterface(Ci.nsIUploadChannel);  
    channel = channel.uploadStream;
    channel.QueryInterface(Ci.nsISeekableStream)
           .seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
    var stream = Cc["@mozilla.org/binaryinputstream;1"]
                   .createInstance(Ci.nsIBinaryInputStream);  
    stream.setInputStream(channel);  
    var postBytes = stream.readByteArray(stream.available());  
    poststr = String.fromCharCode.apply(null, postBytes);  

    //change the poststr

    // poststr=poststr.replace(....);  

    // ERROR HERE - stringStream is not defined
    stringStream.setData(poststr, poststr.length);  
    //changing the postdata  
    channel = channel.QueryInterface(Ci.nsIUploadChannel);  
    channel = channel.uploadStream;  
    channel.QueryInterface(Ci.nsISeekableStream)
           .seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);  
    channel.uploadStream.QueryInterface(Ci.nsIMIMEInputStream);  
    channel.uploadStream.setData(stringStream);  
    channel.requestMethod = "POST";
  }
});

请注意,我只是在您提供的代码中将Components.classes替换为CcComponents.interfaces替换为Ci - 我不确定此代码应该执行的操作但是我很确定它不起作用。