Hi!

I'm trying to develop a Firefox (v2.0.0.6) extension, which should check
(and perhaps modify) the contents of a site having been retrieved,
before it is shown by firefox.

Up to now, I have registered an observer to the http-on-examine-response
event. The observer receives an HttpChannel as subject. I would like to
call subject.asyncOpen but I get an error that the method is unknown.

Error: Component returned failure code: 0x804b000f [nsIChannel.open] =
<unknown>

So, I copy the URI of the channel and have the IOservice create a new
channel. Before that, I have to remove the observer to prevent an
infinite loop since the creating a new channel will emit a new event.

Now I can call asyncOpen with a streamListener and read data from the
input stream. After modifying data, I could write them to disk and open
the file:// url in a new tab, but it would be better if the browser
showed the modified site automatically.

My main question is: How can I modify the data, so the modified web site
is displayed in the browser?

Another question is why I cannot use subject.asyncOpen. Instead I have
to open a new channel to the URI's address, and then call asyncOpen().

Best Regards and thanks in advance,
Steffen

P.S.: Up to now my implementation looks like this:

function Observer() {}

Observer.prototype = {
  observe: function(subject,topic,data){
  subject.QueryInterface(Components.interfaces.nsIChannel);
  observerService.removeObserver(this,"http-on-examine-response");
  dump(subject.URI.spec);
  var ioService =
Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  var scriptableStream =
Components.classes["@mozilla.org/scriptableinputstream;1"].getService(Components.interfaces.nsIScriptableInputStream);
  var channel = ioService.newChannel(subject.URI.spec,null,null);

  channel.asyncOpen(streamListener, null);
//    observerService.addObserver(this, "http-on-examine-response", false);
    dump("opened channel"+"\n");
  },
  QueryInterface: function(iid) {
                if (!iid.equals(Components.interfaces.nsIObserver))
                        throw Components.results.NS_ERROR_NO_INTERFACE;
            dump("object of type nsIObserver");
                return this;
  }
}

var streamListener = {
 data: "",
 onDataAvailable: function( /*nsIRequest*/ request , /*nsISupports*/
context, /*nsIInputStream*/ inputStream , /*PRUint32*/ offset ,
/*PRUint32*/ count) {
  var scriptableInputStream =
Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  scriptableInputStream.init(inputStream);

   this.data += scriptableInputStream.read(count);
   dump("OnDataAvailable:"+ this.data);
   this.data = this.data.replace("Erweiterte","Buh");
   this.data = this.data.replace("Auf","Buh2");
   dump("OnDataAvailable:"+ this.data);
 },
 onStartRequest: function(request, context){   dump("stepped into
onStartRequest" + "\n");},
 onStopRequest: function(request, context, status)
 {
     if (Components.isSuccessCode(status))
         {
           dump("success\n");
         }
     var wm =
Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
     var browserWindow = wm.getMostRecentWindow("navigator:browser");
//       browserWindow.QueryInterface(Components.interfaces.nsIDOMJSWindow);
     var browser = browserWindow.getBrowser();
     browser.addTab("file:///E:/temp/test2.htm", null, null, null, null,
null);
     //browser.removeCurrentTab();
 },
 QueryInterface: function(iid) {
                if (!iid.equals(Components.interfaces.nsIStreamListener))
                        throw Components.results.NS_ERROR_NO_INTERFACE;
            dump("object of type nsIStreamListener");
                return this;
        }
};

// initialization
function NSGetModule() {
  observerService.addObserver(new Observer(),
http-on-examine-response", false);
  dump("register observer\n");
  dump("loaded grid services browser add-on\n");
  return browserModule;
}
_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network

Reply via email to