Skip to content

Instantly share code, notes, and snippets.

@c6fc
Created April 5, 2016 17:16
Show Gist options
  • Save c6fc/3bc7bf4ad365bdf4c9052dcae91005de to your computer and use it in GitHub Desktop.
Save c6fc/3bc7bf4ad365bdf4c9052dcae91005de to your computer and use it in GitHub Desktop.
Intercept Ajax responses and forward them to a separate host
// ==UserScript==
// @name AjaxTest
// @namespace intercept
// @include <site>
// @version 1
// @grant GM_xmlhttpRequest
// ==/UserScript==
function main () {
window.alert = function() {
return console.log.apply(this, arguments);
}
window.stash = Array();
window.XMLHttpRequest.prototype.otherOpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
this.addEventListener("readystatechange", function() {
if (this.readyState == 4) {
console.log(this.responseText);
window.stash.push(this.responseText);
deliver();
}
}, false);
window.XMLHttpRequest.prototype.otherOpen.apply(this, arguments);
};
}
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ main +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
function deliver() {
console.log('delivering...');
while (i = unsafeWindow.stash.shift()) {
console.log('Shifted: [' + i + ']');
GM_xmlhttpRequest({
method: "POST",
url: "endpoint.php",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: 'intel=' + JSON.stringify(i),
onload: function(response) {
console.log(response.responseText);
}
});
}
}
exportFunction(deliver, unsafeWindow, {defineAs: "deliver"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment