We're working with a custom frontend framework that's based on PrimeFaces/JSF.
Lately, we added a display that shows how long until the user gets logged out due to inactivity. To correctly reset the countdown on user activity, I've overwritten the XMLHttpRequest.prototype.send method via JavaScript:
var minutesUntilTimeout = 60;
var logoutAt = minutesUntilTimeout * 60 * 1000 + new Date().getTime();
var send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data) {
send.call(this, data);
logoutAt = minutesUntilTimeout * 60 * 1000 + new Date().getTime();
}
This works correctly as it is.
However, we encountered a problem:
The JS code gets executed on page load, and to work correctly, it is mandatory that XMLHttpRequest.prototype.send contains the original functionality. However, sometimes it still contains our overwritten function from the last JS execution (e.g. when we get to a page via the breadcrumb menu). This breaks the application, since every user action that uses XMLHttpRequest.prototype.send will trigger the overwritten function, that internally calls the var send "original" function, which is the same function in this special case, creating an infinite loop. I think it has to do with the HTTP request, since as far as I remember, the breadcrumbs use GET and all buttons, main menu etc. use POST (but this is just a guess, I'm not that deep into the implementation details of our framework).
Is there any way to ensure that XMLHttpRequest.prototype correctly resets on ALL page changes, aside from simply avoiding the breadcrumbs?
Once again, the rubber duck principle holds true - minutes after posting, I found a workaround. In case anyone ever needs it, I added a case differentiation:
if (typeof XMLHttpRequest.prototype.oldSend !== "function") {
XMLHttpRequest.prototype.oldSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data) {
XMLHttpRequest.prototype.oldSend.call(this, data);
logoutAt = minutesUntilTimeout * 60 * 1000 + new Date().getTime();
}
}