自定义xul:浏览器中页面加载的观察者

时间:2009-10-11 04:10:16

标签: firefox-addon

在我的firefox扩展中,我正在创建一个xul:browser元素。我希望有一个观察者拦截嵌入式浏览器中的任何网址更改,并在新的浏览器选项卡中打开网址(在主浏览器中)。我还想像xul:browser窗口生成的新窗口在标签中打开而不是新的浏览器窗口。

我已经创建了一个可以工作的观察者,但我还不知道如何将该观察者仅应用于xul:browser元素。

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 个答案:

答案 0 :(得分:4)

你可以尝试:

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;
}

我会更新这个以填补空白。

编辑:请参阅http://forums.mozillazine.org/viewtopic.php?p=2772951#p2772951了解如何获取请求的源窗口。

请求http://zenit.senecac.on.ca/wiki/index.php/Support_For_OpenID的取消代码。

有关nsIRequest的详细信息,请参阅http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsIRequest.idl

有关addTab的定义,请参阅http://forums.mozillazine.org/viewtopic.php?p=2404533#p2404533https://developer.mozilla.org/en/XUL/Method/addTab

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

有关如何以适合addTab的形式处理发布数据,请参阅https://developer.mozilla.org/en/Code_snippets/Post_data_to_window#Preprocessing_POST_data

ReadPostFromText和ReadTextFromStream都来自萤火虫(虽然略有修改)