0

Im a beginner in Javascript, and I have the following problem ... can someone tell me what I'm doing wrong ? Thanks !

So, go on Instagram, fill the following code in the console:

var open = window.XMLHttpRequest.prototype.open,
    onReadyStateChange;

function openReplacement(method, url, async, user, password) {
    if (url.match(/https:\/\/i\.instagram\.com\/api\/v1\/feed\/user\/[0-9]{1,16}\/story\//g)) {
        console.log(url)
        console.log(this)
        console.log(this.response)
    }
    return open.apply(this, arguments);
}

window.XMLHttpRequest.prototype.open = openReplacement;

So, when I do this and go on a profile, it is supposed to read and print the api response to the get the story data, and it prints the following thing for the console.log(this):

XMLHttpRequest:
    listeners: {load: Array, error: Array, timeout: Array}
    onabort: null
    onerror: function(t)
    onload: function()
    onloadend: null
    onloadstart: null
    onprogress: null
    onreadystatechange: null
    ontimeout: function()
    readyState: 4
    response: "{\"broadcast\":null,\"reel\":{\"id\":…"
    responseText: "{\"broadcast\":null,\"reel\":{\"id\":…"
    responseType: ""
    responseURL: "https://i.instagram.com/api/v1/feed/user/AN_ID/story/"
    responseXML: null
    status: 200
    statusText: ""
    timeout: 10000
    upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
    withCredentials: true

But when the following line comes console.log(this.response), an empty line is printed. And it does this with every values printed in the this upper. I tried to console.log(Object.keys(this)), but same, an empty line is printed ...

If I do this.test = "test there" and do console.log(this.test), "test there" is well printed.

What am I doing wrong ??? Why isn't it returning me the response value from the this ? Thanks for your help !

hmngwn
  • 17
  • 1
  • 4
    the console lies with objects - at the time `this` is logged, it won't have a value in `this.response`, but when you inspect it in the console, after the request finishes, the console shows the current state of `this` – Bravo Feb 09 '22 at 22:11
  • 1
    a simple illustration of the above comment ... `let o = {}; console.log(o); o.foo = 'bar'` ... you'll see `o` has `foo` property in the console, even though it didn't when you logged it - just as a `XMLHttpRequest` won't have a response when you `.open` - only after you `.send` – Bravo Feb 09 '22 at 22:13

0 Answers0