Redirecting Request (nsiHttpChannel?) in Firefox Extensions

后端 未结 4 1849
旧巷少年郎
旧巷少年郎 2021-02-01 13:17

I\'ve been trying at this for a long time now, and no good results.

var myObserver = {
    observe: function(subject, topic, data)
    {
        if (topic == \"h         


        
4条回答
  •  梦谈多话
    2021-02-01 14:12

    I am under the impression that you cannot do this at this level - I tried a variety of methods of externally "tricking" the code that calls for a nsIHttpChannel to be created (example at end of post).

    What I would recommend is if you want a redirect, contact the channel's owner window (which works 99% of the time), and instruct it to redirect. I know that it's not going to behave the same, but since I don't know exactly why you're doing this, this will externally (to the user) appear to be doing the same thing as what you ask.

    Here's the basics of what I was trying:

    if(aTopic == "http-on-examine-response") {                                                                                     
                var request = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);                                                      
    
                if(!request.URI.spec.match("^http://www.apple.com/")) {                                                          
                    var ios = Components.classes["@mozilla.org/network/io-service;1"]                                                             
                        .getService(Components.interfaces.nsIIOService);                                                                          
                    var ch = ios.newChannel("http://www.apple.com/", null, null);                                                                 
    
                    var listener = {                                                                                                              
                        QueryInterface : XPCOMUtils.generateQI([Ci.nsIChannelEventSink]),                                                         
                        onDataAvailable: function() {},                                                                                           
                        onStopRequest: function() {},                                                                                             
                        onStartRequest: function() {}                                                                                             
                    };                                                                                                                            
    
                    ch.asyncOpen(listener,null);                                                                                                  
    
                    var eventSink = request.notificationCallbacks.getInterface(Ci.nsIChannelEventSink);                                           
                    eventSink.asyncOnChannelRedirect(request,ch,Ci.nsIChannelEventSink.REDIRECT_INTERNAL,function() {});                          
                } 
    

提交回复
热议问题