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 6849277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:58:16+00:00 2026-05-27T00:58:16+00:00

I have a user script (for chrome and FF) that adds significant functionality to

  • 0

I have a user script (for chrome and FF) that adds significant functionality to a page, but has recently been broken because the developers added some AJAX to the page. I would like to modify the script to listen to the pages xmlhttp requests, so that I can update my added content dynamically, based on the JSON formatted responseText that the page is receiving.

A search has turned up many functions that SHOULD work, and do work when run in the console. However they do nothing from the context of a user script.

(function(open) {

    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {

        this.addEventListener("readystatechange", function() {
            console.log(this.readyState);
        }, false);

        open.call(this, method, url, async, user, pass);
    };

})(XMLHttpRequest.prototype.open);

From: How can I intercept XMLHttpRequests from a Greasemonkey script?

This works perfectly in the console, I can change this.readyState to this.responseText and it works great (though in the script I will need it to turn the JSON data into an object, and then let me manipulate it within the userscript. Not just write to the console). However if I paste it into a userscript nothing happens. The xmlhttp requests on the page do not seem to be detected by the event handler in the userscript.

The page doing the requesting is using the jquery $.get() function, if that could have anything to do with it. Though I don’t think it does.

I can’t imagine that there isn’t a way, seems like any userscript running on an AJAX page would want this ability.

  • 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-05-27T00:58:17+00:00Added an answer on May 27, 2026 at 12:58 am

    Since the page uses $.get(), it’s even easier to intercept requests. Use ajaxSuccess().

    This will work in a Greasemonkey(Firefox) script:
    Snippet 1:

    unsafeWindow.$('body').ajaxSuccess (
        function (event, requestData)
        {
            console.log (requestData.responseText);
        }
    );
    

    Assuming the page uses jQuery in the normal way ($ is defined, etc.).

    This should work in a Chrome userscript (as well as Greasemonkey):
    Snippet 2:

    function interceptAjax () {
        $('body').ajaxSuccess (
            function (event, requestData)
            {
                console.log (requestData.responseText);
            }
        );
    }
    
    function addJS_Node (text, s_URL, funcToRun) {
        var D                                   = document;
        var scriptNode                          = D.createElement ('script');
        scriptNode.type                         = "text/javascript";
        if (text)       scriptNode.textContent  = text;
        if (s_URL)      scriptNode.src          = s_URL;
        if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
    
        var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
        targ.appendChild (scriptNode);
    }
    
    addJS_Node (null, null, interceptAjax);
    



    Re:

    “But how then do I get that data to the script? … (So I can) use the data later in the script.”

    This works in Greasemonkey(Firefox); it might also work in Chrome’s Tampermonkey:
    Snippet 3:

    function myAjaxHandler (requestData) {
        console.log ('myAjaxHandler: ', requestData.responseText);
    }
    
    unsafeWindow.$('body').ajaxSuccess (
        function (event, requestData) {
            myAjaxHandler (requestData);
        }
    );
    

    But, if it doesn’t then you cannot share JS information (easily) between a Chrome userscript and the target page — by design.

    Typically what you do is inject your entire userscript, so that everything runs in the page scope. Like so:
    Snippet 4:

    function scriptWrapper () {
    
        //--- Intercept Ajax
        $('body').ajaxSuccess (
            function (event, requestData) {
                doStuffWithAjax (requestData);
            }
        );
    
        function doStuffWithAjax (requestData) {
            console.log ('doStuffWithAjax: ', requestData.responseText);
        }
    
        //--- DO YOUR OTHER STUFF HERE.
        console.log ('Doing stuff outside Ajax.');
    }
    
    function addJS_Node (text, s_URL, funcToRun) {
        var D                                   = document;
        var scriptNode                          = D.createElement ('script');
        scriptNode.type                         = "text/javascript";
        if (text)       scriptNode.textContent  = text;
        if (s_URL)      scriptNode.src          = s_URL;
        if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
    
        var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
        targ.appendChild (scriptNode);
    }
    
    addJS_Node (null, null, scriptWrapper);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script, test.php that has the contents <?php require_once(classes/user.php); echo test;
I have a user script that would be much more useful if it could
I have a greasemonkey user script with this single line of code... window.close(); but
I have a script that calls an application that requires user input, e.g. run
I have a PHP script that creates other PHP files based on user input.
How do I have a Python script that can accept user input and how
I want to write a greasemonkey script/chrome user script that takes any images with
I have a JS script that works on IE8 compatibility mode, FF, Chrome, Opera,
I have written a Chrome extension that adds a char counter to some input
I have a script below that uses $_SERVER['HTTP_USER_AGENT'] because get_browser() is not active on

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.