is it possible with javascript to listen for and capture outgoing HTTP requests? (For example, AJAX calls). Sort of like firebug, etc
-
To clarify, what I want to do is run a website in an iframe and have my JS listen for outgoing http requests that are sent through my browser.Andy Hin– Andy Hin2011-01-12 07:01:54 +00:00Commented Jan 12, 2011 at 7:01
-
You're not going to be able to monitor the whole browser using content JavaScript. That would violate the same-origin policy. To do that, you have to run inside the browser's chrome (including as an extension).Matthew Flaschen– Matthew Flaschen2011-01-12 07:11:23 +00:00Commented Jan 12, 2011 at 7:11
-
What access does an extension give you that you can't do via straight javascript? Thanks :)Andy Hin– Andy Hin2011-01-12 17:15:53 +00:00Commented Jan 12, 2011 at 17:15
3 Answers
Nope, not like firebug. The closest thing you can do, if you're using a framework such as jQuery or Microsoft AJAX.NET, you can be notified when an AJAX request is issued through that specific framework.
For jQuery, that's .ajaxStart()
There are heaps of requests that this won't catch, though, such as all requested assets (script files, images, etc.)
3 Comments
Sure, you can trace AJAX requests on your own pages. Make a wrapper for the XMLHttpRequest constructor that records the information you're interested in.
EDIT: An extension lets you observe internal browser operations. For example, you can see in Firebug's source that it observes all of Firefox's HTTP topics. These are "http-on-modify-request", "http-on-examine-response", and "http-on-examine-cached-response."
Comments
If you want to capture all HTTP requests, I would suggest using Charles. Its an awesome tool which works as proxy and gives result in human readable form.
Shows Request, Response, Time Chart. The request is shown with headers. Response is shown with headers along with content. Worth trying it.