Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a website is making X http request with javascript

I've been searching for a while but all I could find was examples on how to actually make those requests...

What I'm trying to do is to check if a website is making an http request after I click a button to X site

Ex. http://test.s3.amazonaws.com/test/1.txt

I'm guessing there a way to get all the outgoing requests and then filter the list but I can't even get the requests

What I've done so far is just inserting the eventlistener to check that X button has been clicked and then is when I'd like to check for those requests.

<script language="javascript">
document.getElementById("button").addEventListener('click', buttonClicked, true);

function buttonClicked(){
    -->Check requests made after button is clicked.<--
}
</script>
like image 958
Alvaro Arregui Avatar asked Sep 05 '25 03:09

Alvaro Arregui


1 Answers

I made a basic script for this a while ago, basically you just wrap the built-in XMLHttpRequest.

(function() {
  'use strict';
  var oldXHR, stateChangeHandler, prop;

  oldXHR = window.XMLHttpRequest;

  stateChangeHandler = function (evt) {
    switch (this.readyState) {
      case oldXHR.OPENED:
        console.log('Request was made', this, evt);
        break;
      case oldXHR.DONE:
        console.log('Request finished', this, evt);
        break;
    }
  };

  function newXHR() {
    var xhr = new oldXHR();
    xhr.addEventListener('readystatechange', stateChangeHandler);
    return xhr;
  }

  // Copy original states and toString
  for (prop in oldXHR)
    newXHR[prop] = oldXHR[prop];

  window.XMLHttpRequest = newXHR;
})();
like image 170
bennedich Avatar answered Sep 07 '25 21:09

bennedich