我正在尝试监视/拦截所有http流量,并将所有内容的记录推送到日志中。这适用于访问初始页面加载,但它没有观察到任何其他内容(xhr,css,图像等)。
我已按照@ http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/的建议尝试了侦听器方法,但这也没有给我xhr(只是来自服务器的完整html回复,而不是任何ajax或其他请求)。
这是我的观察员代码:
var httpLogger = {
http_methods_list: [
'http-on-examine-response',
'http-on-examine-cached-response',
'http-on-examine-merged-response'
],
is_enabled: false,
check_url_list: [ "*" ],
log: {},
enable: function() {
if (! this.is_enabled) {
this.is_enabled = true;
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
for (var i=0; i < this.http_methods_list.length; ++i) {
observerService.addObserver( this, this.http_methods_list[i], false );
}
}
},
disable: function() {
if (this.is_enabled) {
this.is_enabled = false;
var observerService = fxdriver.moz.getService("@mozilla.org/observer-service;1", "nsIObserverService");
for (var i=0; i < this.http_methods_list.length; ++i) {
observerService.removeObserver( this, this.http_methods_list[i], false );
}
}
},
observe: function(subject, topic, data) {
try {
if (this.http_methods_list.indexOf(topic) != -1) {
subject = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
if (subject.URI) {
url = subject.URI.spec;
} else if (subject.originalURI) {
url = subject.originalURI.spec;
} else {
this._pushToLog( "debug", {event: "no_URI"} );
return;
}
if (this._urlIsKosher(subject.URI.spec)) {
dom_window = this._getDomWindow(subject);
event_log = {
request: this._getRequestFromSubject(subject),
response: this._getResponseFromSubject(subject),
responseStatus: subject.responseStatus,
responseStatusText: subject.responseStatusText,
requestSucceeded: subject.requestSucceeded,
requestMethod: subject.requestMethod,
status: subject.status,
name: subject.name
};
if (dom_window) {
window_handle = this._getWindowHandle(dom_window);
if (window_handle) {
this._pushToLog( window_handle, event_log );
} else {
event_log.event = "no_handle";
this._pushToLog( "debug", event_log );
}
} else {
event_log.event = "no_window";
this._pushToLog( "debug", event_log );
}
}
}
} catch (err) {
this._pushToLog( "debug", {error: err.message} );
}
},
/* other helper functions like this._pushToLog(), this._urlIsKosher(), etc. */
}
例如,如果我浏览到https://developer.yahoo.com/yql/console/,我的记录器将记录以下内容:
{
request: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
Accept-Encoding: 'gzip, deflate',
Accept-Language: 'en-US,en;q=0.5',
Connection: 'keep-alive',
Host: 'developer.yahoo.com',
User-Agent: 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'
},
response: {
Age: '3',
Cache-Control: 'private, no-cache',
Connection: 'keep-alive',
Content-Encoding: 'gzip',
Content-Type: 'text/html; charset=utf-8',
Date: 'Fri, 15 May 2015 16:25:28 GMT',
P3P: 'policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAODSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"',
Server: 'ATS',
Set-Cookie: 'B=e2jdjjpalc7fo&b=3&s=ei; expires=Mon, 15-May-2017 16:25:29 GMT; path=/; domain=.yahoo.com',
Transfer-Encoding: 'chunked',
Vary: 'Accept-Encoding',
Via: 'http/1.1 r36.ycpi.bf1.yahoo.net (ApacheTrafficServer [cMsSf ])',
X-Frame-Options: 'SAMEORIGIN'
},
responseStatus: 200,
responseStatusText: 'OK',
requestSucceeded: true,
requestMethod: 'GET',
status: 0,
name: 'https://developer.yahoo.com/yql/console/'
}
但是当我点击yahoo-yql页面上的“Test”按钮(你可以在firebug中看到xhr)时,我的记录器不记录任何内容(“debug”或其他)。
任何建议都会非常有用 - 我宁愿坚持这一点。谢谢!