3

Is it possible to create a Javascript and include it in a web page that the function of this JS is to "catch" all GET request or any other Ajax calls made from any other Javascript on the page? Either to log it or just plain show it in a Alert box.

The "other Javacript" that will be executing GET or Ajax calls is arbitrary. I mean I have no control over that in terms of what it is.

And once caught I need to check which Javascript executing which GET or Ajax calls.

Is this possible?

6
  • Do you need this for debugging? All major browsers have this built in. Commented Mar 29, 2012 at 7:28
  • For now since it seems that its not easy with plain Javascript maybe I can use browser plugin, however Firebug does not show which Javascript initiated the GET method for example... Commented Mar 29, 2012 at 7:32
  • ...but my requirement is not really for debugging, but for actual application function. Commented Mar 29, 2012 at 8:44
  • ...but my requirement is not really for debugging, but for actual application function. Commented Mar 29, 2012 at 8:44
  • Then test @Jasd's solution. It's probably the right direction. Though you'll have to make sure your code runs before the first ajax request. Commented Mar 29, 2012 at 8:47

2 Answers 2

12

Try this snippet. It extends the send function so that you can execute something before or after the real sending.

XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(body) {
    // Do something...
    this.reallySend(body);
};
var req = new XMLHttpRequest();
req.open("GET", "any.html", true);
req.send(null);
Sign up to request clarification or add additional context in comments.

5 Comments

This worked and I was able to catch Ajax call, however I did not caught the request for the HTML I was expecting probably because its not Ajax call, do you know how to intercept GET events caused by Javascript?
@xybrek You can use this approach as a template. What you have to do is adept this to every method javascript has to make requests (e.g. the Image Object, <script>-elements, and many more).
@xybrek Unfortunately I don't know such an event. Maybe it's best to follow Yoshi's advice to implement the solution for each element which can cause the browser to make a request.
@Yoshi can you site some example for your approach?
@xybrek sort of similar question: stackoverflow.com/a/9676178/697154 hope it helps
1
const nativeOpen = XMLHttpRequest.prototype.open;

const proxiedOpen = function () {
  if (arguments[1].includes('youUrl.com')) {
    // do some ...
  }
  nativeOpen.apply(this, arguments);
};

Refer to the answer:

Intercept AND CHANGE the results of an ajax call

2 Comments

why answer a 8 year old question which was resolved...?
I searched for related questions and found this question, so..., more reference answers are not a bad thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.