My simple goal is to intercept xmlHttpRequests sent by a page and send them to my local server for logging in a text file. However, Ajax calls do not work in event listeners. I have tried various solutions all day long without success. Here is the code snippet:
var ajaxQueue = [];
var processAjaxQueue = function(){
if (ajaxQueue.length > 0) {
for (ajax in ajaxQueue) {
var obj = ajaxQueue[ajax];
setTimeOut(function(){GM_xmlhttpRequest(obj);},0);
}
ajaxQueue = [];
}
}
setInterval(function(){
processAjaxQueue();
}, 100);
function gmAjax(obj){
ajaxQueue.push(obj);
}
function sendData(data)
{
setTimeout(function() {gmAjax({
method: "POST",
url: "http://127.0.0.1/log/index.php",
data: "price="+data,
headers: {"Content-Type": "application/x-www-form-urlencoded"},
onload: function(response){alert('Loaded! '+response.responseText);}
});}, 0);
}
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("readystatechange", function() {
console.log(this.readyState);
if(this.readyState == 4)
{
alert(this.responseText);
sendData(this.responseText);
}
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
I am struggling to understand what the issue might be. The concept of gmAjax stems from a source where it was mentioned that greasemonkey scripts execute and stop abruptly, making it impossible to use addevent listener.
No errors are visible in the error console and the request works fine when not inside the event listener.
Any assistance would be highly appreciated. Thank you!