Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 395475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:27:28+00:00 2026-05-12T16:27:28+00:00

In my firefox extension I’m creating a xul:browser element. I want to have an

  • 0

In my firefox extension I’m creating a xul:browser element. I want to have an observer that intercepts any url changes within the embedded browser and opens the url in a new browser tab (in the main browser). I’d also like new windows spawned by the xul:browser window to open in a tab instead of a new browser window.

I’ve created an observer which works, but I don’t yet know how to apply that observer only to the xul:browser element.

function myFunction(){
   var container = jQuery("#container")[0];
   var new_browser_element = document.createElement('browser');
   container.appendChild(new_browser_element);

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


var myObserver = {
   observe: function(aSubject, aTopic, aData){
      if (aTopic != 'http-on-modify-request'){
         aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
         // alert(aSubject.URI.spec);
        // Now open url in new tab
      } 
   },

   QueryInterface: function(iid){
      if (!iid.equals(Components.interfaces.nsISupports) &&
      !iid.equals(Components.interfaces.nsIObserver))
      throw Components.results.NS_ERROR_NO_INTERFACE;

      return this;
   }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:27:28+00:00Added an answer on May 12, 2026 at 4:27 pm

    You could try:

    var myObserver = {
       observe: function(aSubject, aTopic, aData){
          if (aTopic == 'http-on-modify-request')
                  {
                    aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
                    var url = aSubject.URI.spec;
                    var postData ;
                    if (aSubject.requestMethod.toLowerCase() == "post") 
                    {
                      var postText = this.readPostTextFromRequest(request);
                      if (postText)
                      {
                        var dataString = parseQuery(postText);
                        postData = postDataFromString(dataString);
                      }
                    }
    
                    var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
    
                    var interfaceRequestor =   oHttp.notificationCallbacks.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
                    var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); 
    
    
                    //check if it is one of your mini browser windows
                    if (jQuery(DOMWindow).hasClass('mini_browser')) 
                    {
    
                      openInTab(url, postData);
    
                      var request = aSubject.QueryInterface(Components.interfaces.nsIRequest);
                      request.cancel(Components.results.NS_BINDING_ABORTED);
    
                    }
    
          } 
       },
    
       QueryInterface: function(iid){
          if (!iid.equals(Components.interfaces.nsISupports) &&
              !iid.equals(Components.interfaces.nsIObserver))
            throw Components.results.NS_ERROR_NO_INTERFACE;
    
          return this;
       },
    
       readPostTextFromRequest : function(request) {
         var is = request.QueryInterface(Components.interfaces.nsIUploadChannel).uploadStream;
         if (is)
         {
           var ss = is.QueryInterface(Components.interfaces.nsISeekableStream);
           var prevOffset;
           if (ss)
           {
             prevOffset = ss.tell();
             ss.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);
           }
    
           // Read data from the stream..
           var charset = "UTF-8";
           var text = this.readFromStream(is, charset, true);
    
           // Seek locks the file so, seek to the beginning only if necko hasn't read it yet,
           // since necko doesn't seek to 0 before reading (at lest not till 459384 is fixed).
           if (ss && prevOffset == 0) 
             ss.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);
    
           return text;
         }
         else {
           dump("Failed to Query Interface for upload stream.\n");
         }
       }
       return null;
     },
    
     readFromStream : function(stream, charset, noClose)    
     {
        var sis = Components.classes["@mozilla.org/binaryinputstream;1"]
                         .getService(Components.interfaces.nsIBinaryInputStream);
    
        sis.setInputStream(stream);
    
        var segments = [];
        for (var count = stream.available(); count; count = stream.available())
          segments.push(sis.readBytes(count));
    
    if (!noClose)   
          sis.close();
    
        var text = segments.join("");
        return text;
      }
    };
    
    function openInTab(url, postData)
    {
      var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
            .getService(Components.interfaces.nsIWindowMediator);
      var recentWindow = wm.getMostRecentWindow("navigator:browser");
      if (recentWindow) 
      {
        // Use an existing browser window, open tab and "select" it
        recentWindow.gBrowser.selectedTab = recentWindow.gBrowser.addTab(url, null, null, postData);
      } 
    }
    
    function parseQuery() {
      var qry = this;
      var rex = /[?&]?([^=]+)(?:=([^&#]*))?/g;
      var qmatch, key;
      var paramValues = {};
      // parse querystring storing key/values in the ParamValues associative array
      while (qmatch = rex.exec(qry)) {
        key = decodeURIComponent(qmatch[1]);// get decoded key
        val = decodeURIComponent(qmatch[2]);// get decoded value
    
        paramValues[key] = val;
      }
      return paramValues;
    }
    
    function postDataFromString(dataString)
    {
      // POST method requests must wrap the encoded text in a MIME
      // stream
      var stringStream = Components.classes["@mozilla.org/io/string-input-stream;1"]
                   .createInstance(Components.interfaces.nsIStringInputStream);
      if ("data" in stringStream) // Gecko 1.9 or newer
        stringStream.data = dataString;
      else // 1.8 or older
        stringStream.setData(dataString, dataString.length);
    
      var postData = Components.classes["@mozilla.org/network/mime-input-stream;1"].
               createInstance(Components.interfaces.nsIMIMEInputStream);
      postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
      postData.addContentLength = true;
      postData.setData(stringStream);
    
      return postData;
    }
    

    I’ll update this to fill in the blanks in a bit.

    edit: see http://forums.mozillazine.org/viewtopic.php?p=2772951#p2772951 for how to get the source window of a request.

    Request cancellation code from http://zenit.senecac.on.ca/wiki/index.php/Support_For_OpenID.

    see http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsIRequest.idl for details on nsIRequest.

    See http://forums.mozillazine.org/viewtopic.php?p=2404533#p2404533 and https://developer.mozilla.org/en/XUL/Method/addTab for the definition of addTab.

    parseQuery comes from http://blog.strictly-software.com/2008/10/using-javascript-to-parse-querystring.html.

    See https://developer.mozilla.org/en/Code_snippets/Post_data_to_window#Preprocessing_POST_data for how to process post data in a form suitable for addTab.

    ReadPostFromText and ReadTextFromStream both come from firebug (though slightly modified)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a firefox extension. I want to get the URL of the
I am writing a Firefox extension. I have setup an overlay for chrome://browser/content/browser.xul and
From a Firefox extension is there a way to get the XUL iframe/browser that
I'm making a Firefox extension and I want to position a XUL panel element
I'm developing a Firefox extension, and I have a toolbar button that displays an
I am creating a Firefox Extension...what would be the javascript to open a URL
I have a working firefox extension that currently consists of a button. When I
In the Firefox extension I'm writing, I have a variable containing some data that
I have a Firefox extension that adds several buttons to the nav-bar toolbar using
I am creating a firefox extension. I want to get a reference to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.