Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda