Created
April 5, 2016 17:16
-
-
Save c6fc/3bc7bf4ad365bdf4c9052dcae91005de to your computer and use it in GitHub Desktop.
Intercept Ajax responses and forward them to a separate host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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