Firefox - 确定谁发起了http请求

时间:2015-06-30 04:36:27

标签: javascript firefox firefox-addon firefox-addon-sdk firefox-developer-tools

在我当前的插件中,我使用http-on-modify-request

拦截Firefox上的所有http请求

实际上,使用我当前的代码,我能够分离来自网页/标签的请求以及所有其他来自的请求(例如,RSS提要更新,扩展管理器请求,来自XPCOM组件的XHR请求等)

我想确定除了网络流量之外发起请求的人不仅仅是整个群体吗? (RSS提要更新,扩展管理器请求,XPCOM组件的XHR请求等)

示例:自定义变量requestRequestor具有标识特定插件或RSS更新等的值。

我发现这个类似的question没有解决方案。

识别整个群组的当前代码(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
            }
        }
    }
};

2 个答案:

答案 0 :(得分:1)

截至2020年6月,尚无官方方法/方式来实现http请求请求者的过滤/识别。

当前唯一的可能性是对问题的代码执行什么操作,它将请求从网页/选项卡和其他Firefox组件(提要更新,扩展请求,来自XPCOM组件的XHR请求等)中分离出来。

正如评论中提到的,这是Firefox的内部限制。当前Firefox的核心代码未实现请求者跟踪,因此不知道是谁发起了请求以及原因。了解Chrome开发人员工具got recently this feature可能会很有用。

答案 1 :(得分:0)

您需要查看HTTP标头内的USER AGENT。它提供了有关客户端系统的详细信息。

希望这会有所帮助:

https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx(了解用户代理字符串),

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_nav_all(用户代理的例子)

P.S:我想将其作为评论发布,但我没有看到该选项。