Skip to content

Instantly share code, notes, and snippets.

@ohasy
Forked from icodejs/ajaxListener.js
Last active April 13, 2023 12:17
Show Gist options
  • Save ohasy/2abc276c712ab3a20e7c2efcc0623c6b to your computer and use it in GitHub Desktop.
Save ohasy/2abc276c712ab3a20e7c2efcc0623c6b to your computer and use it in GitHub Desktop.
JS: Listen to ajax call and download quickly expiring presigned s3 urls (do not paste twice in the console)
var openOrig = window.XMLHttpRequest.prototype.open,
sendOrig = window.XMLHttpRequest.prototype.send,
onReadyStateChange;
function openReplacement(method, url, async, user, password) {
var syncMode = async !== false ? 'async' : 'sync';
console.warn(
'Preparing ' +
syncMode +
' HTTP request : ' +
method +
' ' +
url
);
this.__url = url;
this._windowRef = window;
return openOrig.apply(this, arguments);
}
function sendReplacement(data) {
console.warn('Sending HTTP request data : ', data);
if(this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
this.onreadystatechange = onReadyStateChangeReplacement;
return sendOrig.apply(this, arguments);
}
function onReadyStateChangeReplacement() {
console.warn('HTTP request ready state changed : ' + this.readyState);
if (this.readyState == XMLHttpRequest.DONE) {
const response = JSON.parse(this.responseText);
console.log('response : ',this.__url,response);
if (this.__url.includes("/object?"))
{
[].concat(response.data).forEach(data => {
if (data.component) {
const s3Url = data.component.content;
window.open.call(window, s3Url)
}
})
}
}
if(this._onreadystatechange) {
return this._onreadystatechange.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
@ohasy
Copy link
Author

ohasy commented Apr 13, 2023

For changing the response :


var openOrig = window.XMLHttpRequest.prototype.open,
    sendOrig = window.XMLHttpRequest.prototype.send,
    onReadyStateChange;

function openReplacement(method, url, async, user, password) {
    var syncMode = async !== false ? 'async' : 'sync';
    console.warn(
        'Preparing ' +
        syncMode +
        ' HTTP request : ' +
        method +
        ' ' +
        url
    );
    this.__url = url;
    this._windowRef = window;
    return openOrig.apply(this, arguments);
}

function sendReplacement(data) {
    console.warn('Sending HTTP request data : ', data);

    if(this.onreadystatechange) {
        this._onreadystatechange = this.onreadystatechange;
    }
    this.onreadystatechange = onReadyStateChangeReplacement;
    return sendOrig.apply(this, arguments);
}

function onReadyStateChangeReplacement() {
    console.warn('HTTP request ready state changed : ' + this.readyState);

    if (this.readyState == XMLHttpRequest.DONE) {
        const response = JSON.parse(this.responseText);
        console.log('response : ',this.__url,response);
        if (this.__url.includes("/graphql"))
        {
                console.log('response', response)
        }
    }

    if(this._onreadystatechange) {
        return this._onreadystatechange.apply(this, arguments);
    }
}

window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment