2

I have a website that is not mine. There are lots of JavaScript functions that make ajax calls. I wonder if there is a possibility to modify those functions so after successful ajax request my JavaScript function is called. Maybe there is possibility to inject another handler of ajax queries result codes?

Update

Maybe I should add something more... This ajax calls are realised with use of xajax library. I have found such a piece of code:

<script type="text/javascript" charset="UTF-8">
/* <![CDATA[ */
try { if (undefined == xajax.config) xajax.config = {}; } catch (e) { xajax = {}; xajax.config = {}; };
xajax.config.requestURI = "xajax_loader.php";
xajax.config.statusMessages = false;
xajax.config.waitCursor = true;
xajax.config.version = "xajax 0.5 rc1";
xajax.config.legacy = false;
xajax.config.defaultMode = "asynchronous";
xajax.config.defaultMethod = "POST";
/* ]]> */
</script> 

I do not know xajax library, but maybe there is a way to add something to config so requests after success calls JavaScript functions?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
karex
  • 221
  • 1
  • 6
  • 14
  • You mean, so they get executed for every user of the site? Not without hacking the server, no. – Pekka Jun 12 '13 at 07:44
  • No, no. Just for me - no hacking ;) I want for example to inject code that after every ajax success plays a sound or in order to make it simpler - shows alert dialog. – karex Jun 12 '13 at 07:47
  • 2
    If it's just for you, you might consider a chrome extension: http://stackoverflow.com/questions/8559482/intercept-javascript-requests-with-chrome-extension – izb Jun 12 '13 at 07:49
  • I want it to run on android default browser, so use of (chrome) extensions isn't an option. – karex Jun 12 '13 at 07:52
  • What do you _actually_ want to achieve? Inline replacing site code is rather involved... – Koterpillar Jun 12 '13 at 08:14
  • As I wrote - I want to run a javascript (which for example plays a sound) after completion of certain (or each) ajax request. – karex Jun 12 '13 at 08:26
  • Sounds like a bookmarklet – jantimon Jun 12 '13 at 08:33
  • I guess this is what I want, but for now I do not have possibility to check it. http://community.xajax-project.org/topic/9009/how-to-get-a-success-callback-through-xajax/ Anyone can tell if it will work? – karex Jun 12 '13 at 12:03

2 Answers2

1

It is possible to replace javascript function definitions. Here is some simple code:

<script>
   function xyz() {alert('xyz');}
</script>
<body>
    <button onclick="xyz();">Run xyz</button>
    <button onclick="alert(xyz);">Show xyz</button>
    <button onclick="xyz=function(){alert('xxxxyz');};">Replace xyz</button>
</body>

I created a fiddle http://jsfiddle.net/k565L/ to try this out. But I do not know if it is possible to replace cross-site javascript functions.

0

The source code is available here: https://github.com/Xajax/Xajax/blob/master/xajax_js/xajax_core_uncompressed.js

You might consider doing something like this:

xajax.origCompleteResponse = xajax.completeResponse;
xajax.completeResponse = function(oRequest) {
    // Here you could do anything you want.
    // For example check the request object:
    console.log(oRequest);
    // And then return a call to the original function:
    return xajax.origCompleteResponse.apply(xajax.origCompleteResponse, arguments);
};

Another way of doing the same thing:

(function() {
    var super = xajax.completeResponse; // You can use any variable name you like.
    xajax.completeResponse = function(oRequest) {
        // Here you could do anything you want.
        // For example check the request object:
        console.log(oRequest);
        // And then return a call to the original function:
        return super.apply(super, arguments);
    };
}());

See definition here: https://github.com/Xajax/Xajax/blob/master/xajax_js/xajax_core_uncompressed.js#L3666

demux
  • 4,544
  • 2
  • 32
  • 56