Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Monkey-patching XMLHttpRequest.send for special url

I'm trying to create an http-interceptor that will allow to add header to requests sent from within third-party app. I'm monkey-patching XMLHttpRequest.send

const origSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function() {
    this.setRequestHeader("A-Header", "Value");
    return origSend.apply(this, [].slice.call(arguments));
};

The problem is that I don't need that header in other requests, still I don't see how can I access request url (to check if request is made from third-party lib). How can I make this interceptor work only in case there's a substring in url?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you also monkey-patch the .open() method, you can store the passed url in the instance and read it later:

const origOpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
  this.url = arguments[1];
  return origOpen.apply(this, [].slice.call(arguments));
};

const origSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function() {
  if (this.url) {
    console.log("url found:", this.url);
      this.setRequestHeader("A-Header", "Value");
  }
  // prevent error in snippet, uncomment next line
  // return origSend.apply(this, [].slice.call(arguments));
};

const xhr = new XMLHttpRequest();
xhr.open("get", "https://stackoverflow.com");
xhr.send();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!