Reputation: 23
How can I compile this into a firefox addon? I have tried to use firefox's addon builder located at https://builder.addons.mozilla.org but when I click test it says "XPI not built". Can anybody tell me how I may turn this code into a function firefox extension? Thank you.
//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();
Upvotes: 1
Views: 2048
Reputation: 57651
The example you have there is meant for a classic (XUL-based) extension, not for the Add-on SDK. You can still add that observer using the low-level observer-service
API. Also, SDK-based extensions don't usually have access to the Components
object, it requires chrome authority. Add the following code to your main.js
file:
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";
}
});
Note that I merely replaced Components.classes
by Cc
and Components.interfaces
by Ci
in the code you provided - I'm not sure what this code is supposed to do but I am quite sure that it doesn't work.
Upvotes: 3