1

我怎样才能把它编译成一个firefox插件?我曾尝试使用位于 https://builder.addons.mozilla.org的 Firefox 插件构建器,但是当我单击测试时,它显示“XPI 未构建”。谁能告诉我如何将这段代码变成一个函数 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 的插件,并且没有提供太多示例,我将如何为此创建扩展。
4

1 回答 1

3

您那里的示例适用于经典(基于 XUL)扩展,而不适用于附加 SDK。您仍然可以使用低级observer-serviceAPI添加该观察者。此外,基于 SDK 的扩展通常无法访问该Components对象,它需要chrome 权限。将以下代码添加到您的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.classesCcand Components.interfacesby Ci- 我不确定这段代码应该做什么,但我很确定它不起作用。

于 2012-08-31T08:24:21.730 回答