Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:06:44+00:00 2026-06-11T20:06:44+00:00

How would go about monkey patching the XMLHTTPRequest ‘s onreadystatechange function. I’m trying to

  • 0

How would go about monkey patching the XMLHTTPRequest‘s onreadystatechange function. I’m trying to add a function that would be called when every ajax request made from a page come back.

I know this sounds like a terrible idea, but the use case is quite peculiar. I want to use a certain SDK with a console (jqconsole) but show status and results from ajax calls within the console without modifying the external SDK.

I’ve looked at this post which had great info, but nothing on monkey patching the callback which seem to exceed my JavaScript skills.

P.S Can’t use jQuery since it only supports ajax calls made from jQuery not from XMLHTTPRequests directly which is the case here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:06:45+00:00Added an answer on June 11, 2026 at 8:06 pm

    To monkey-patch XMLHttpRequests, you need to know how an AJAX request is generally constructed:

    1. Constructor invocation
    2. Preparation the request (setRequestHeader(), open())
    3. Sending the request (.send).

    General-purpose patch

    (function(xhr) {
        function banana(xhrInstance) { // Example
            console.log('Monkey RS: ' + xhrInstance.readyState);
        }
        // Capture request before any network activity occurs:
        var send = xhr.send;
        xhr.send = function(data) {
            var rsc = this.onreadystatechange;
            if (rsc) {
                // "onreadystatechange" exists. Monkey-patch it
                this.onreadystatechange = function() {
                    banana(this);
                    return rsc.apply(this, arguments);
                };
            }
            return send.apply(this, arguments);
        };
    })(XMLHttpRequest.prototype);
    

    The previous assumed that onreadystatechange was assigned to the onreadystatechange handler. For simplicity, I didn’t include the code for other events, such as onload. Also, I did not account for events added using addEventListener.

    The previous patch runs for all requests. But what if you want to limit the patch to a specific request only? A request with a certain URL or async flag and a specific request body?

    Conditional monkey-patch

    Example: Intercepting all POST requests whose request body contains “TEST”

    (function(xhr) {
        function banana(xhrInstance) { // Example
            console.log('Monkey RS: ' + xhrInstance.readyState);
        }
        // 
        var open = xhr.open;
        xhr.open = function(method, url, async) {
            // Test if method is POST
            if (/^POST$/i.test(method)) {
                var send = this.send;
                this.send = function(data) {
                    // Test if request body contains "TEST"
                    if (typeof data === 'string' && data.indexOf('TEST') >= 0) {
                        var rsc = this.onreadystatechange;
                        if (rsc) {
                            // Apply monkey-patch
                            this.onreadystatechange = function() {
                                banana(this);
                                return rsc.apply(this, arguments);
                            };
                        }
                    }
                    return send.apply(this, arguments);
                };
            }
            return open.apply(this, arguments);
        };
    })(XMLHttpRequest.prototype);
    

    The main techniques used is the transparent rewrite using…

    var original = xhr.method; 
    xhr.method = function(){
        /*...*/;
        return original.apply(this, arguments);
    };
    

    My examples are very basic, and can be extended to meet your exact wishes. That’s up to you, however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I would go about altering the HTML that
I am wondering how I would go about performing a certain function if a
I am doing some monkey patching in some of Ruby classes and I would
Consider this about the up-coming Iron Python implementation. In theory it would allow monkey
I would like 'about' to route to 'abouts/1' I tried this: match 'about' =>
Any ideas how I would go about writing a javascript method to insert an
Just a quick question about how you would go about implementing this. I want
I was wondering how one would go about developing a website from scratch with
I'm wondering how i would go about making the following application: a user signs
Anyone have any idea how I would go about converting a timestamp in milliseconds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.