Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
848 B
2
Indexable
Never
// Define a function to handle the XHR state change
function trackXHR() {
  // Get the XHR object from the event target
  var xhr = event.target;

  // Check if the request has completed and if it's an external request
  if (xhr.readyState === 4 && xhr.responseURL.startsWith('http')) {
    // Log the URL and response text or status code
    console.log('XHR request to ' + xhr.responseURL + ' completed with status code ' + xhr.status + ' and response:');
    console.log(xhr.responseText);
  }
}

// Override the XHR open method to add the event listener
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
  // Add the event listener to the XHR object
  this.addEventListener('readystatechange', trackXHR);

  // Call the original open method with the same arguments
  origOpen.apply(this, arguments);
};