17

在我目前的插件我攔截在Firefox的所有HTTP請求與http-on-modify-request火狐 - 確定是誰發起的HTTP請求

我目前的代碼

其實我能夠單獨從網頁/標籤來請求並從未來的所有其它(例如,RSS提要更新,擴展管理器請求,來自XPCOM組件的XHR請求等)

我想確定誰發起一個請求,除了網絡流量,精確度不僅僅是整個組(RSS訂閱更新,擴展經理請求,來自XPCOM組件的XHR請求等)?

示例:自定義變量requestRequestor將有一個值來識別

餘富爾德這個silimar問題Identify requests coming from PageWorker沒有一個解決方案的特定插件或RSS更新等。

當前的代碼來識別整組(getting the browser that fires the http-on-modify-request notification)是類似於:

Components.utils.import('resource://gre/modules/Services.jsm'); 
Services.obs.addObserver(httpObs, 'http-on-modify-request', false); 
//Services.obs.removeObserver(httpObs, 'http-on-modify-request'); //uncomment this line, or run this line when you want to remove the observer 

var httpObs = { 
    observe: function (aSubject, aTopic, aData) { 
     if (aTopic == 'http-on-modify-request') { 
      /*start - do not edit here*/ 
      var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel); //i used nsIHttpChannel but i guess you can use nsIChannel, im not sure why though 
      var interfaceRequestor = oHttp.notificationCallbacks.QueryInterface(Components.interfaces.nsIInterfaceRequestor); 
      //var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); //not to be done anymore because: https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //instead do the loadContext stuff below 
      var loadContext; 
      try { 
       loadContext = interfaceRequestor.getInterface(Components.interfaces.nsILoadContext); 
      } catch (ex) { 
       try { 
        loadContext = aSubject.loadGroup.notificationCallbacks.getInterface(Components.interfaces.nsILoadContext); 
        //in ff26 aSubject.loadGroup.notificationCallbacks was null for me, i couldnt find a situation where it wasnt null, but whenever this was null, and i knew a loadContext is supposed to be there, i found that "interfaceRequestor.getInterface(Components.interfaces.nsILoadContext);" worked fine, so im thinking in ff26 it doesnt use aSubject.loadGroup.notificationCallbacks anymore, but im not sure 
       } catch (ex2) { 
        loadContext = null; 
        //this is a problem i dont know why it would get here 
       } 
      } 
      /*end do not edit here*/ 
      /*start - do all your edits below here*/ 
      var url = oHttp.URI.spec; //can get url without needing loadContext 
      if (loadContext) { 
       var contentWindow = loadContext.associatedWindow; //this is the HTML window of the page that just loaded 
       //aDOMWindow this is the firefox window holding the tab 
       var aDOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow); 
       var gBrowser = aDOMWindow.gBrowser; //this is the gBrowser object of the firefox window this tab is in 
       var aTab = gBrowser._getTabForContentWindow(contentWindow.top); //this is the clickable tab xul element, the one found in the tab strip of the firefox window, aTab.linkedBrowser is same as browser var above //can stylize tab like aTab.style.backgroundColor = 'blue'; //can stylize the tab like aTab.style.fontColor = 'red'; 
       var browser = aTab.linkedBrowser; //this is the browser within the tab //this is what the example in the previous section gives 
       //end getting other useful stuff 
      } else { 
       Components.utils.reportError('EXCEPTION: Load Context Not Found!!'); 
       //this is likely no big deal as the channel proably has no associated window, ie: the channel was loading some resource. but if its an ajax call you may end up here 
      } 
     } 
    } 
}; 

感謝您的時間

+3

哇這裏奇妙的工作,你看到這個話題:http://stackoverflow.com/questions/27483651/firefox-addon-monitoring-network/27492685#27492685它更多的一個話題,你可能有助於,我很感興趣,看看你是如何識別來源到目前爲止。 – Noitidart

+1

感謝bro爲你的鏈接提供了關於firefox請求的更多信息等等,我的插件會更好,工作在高級安全插件上,雖然不容易,(這只是插件的業餘愛好的一個愛好;)我會讓你知道在github上) – intika

+0

我會在後面鏈接的問題上發佈我的代碼;) – intika

回答