Greasy Fork is available in English.

Discussions » Development

gm_xmlhttprequest vs xmlhttprequest

§
Posted: 08-11-2015

gm_xmlhttprequest vs xmlhttprequest

I've across something a bit weird.

GM_xmlhttpRequest({
    method: "GET",
    url: url,
    onload: function (xhr) {
        var data = eval(xhr.responseText);
        console.log(data);
    }
});

vs

var xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.onload = function () {
    var data = eval(xhr.responseText);
    console.log(data);
};
xhr.send();

For all intensive purposes, these two functions should return the same response text.(Cross origin is not an issue.) However, when I check the console log, the response for GM_xmlhttpRequest is incorrect. It is near identical but not quite the same as the response I get form XMLHttpRequest.

Any ideas on what might be causing this?

woxxomMod
§
Posted: 08-11-2015

I guess different request headers are sent by default. Try checking it in the inspector in the network tab or use Fiddler app.

§
Posted: 08-11-2015

Any example outputs? What is the url you have used for testing?

btw, I think is better to use JSON.parse than eval.

§
Posted: 09-11-2015
Edited: 09-11-2015

Try this: http://www.dm5.com/m129373/chapterfun.ashx?cid=129373&page=1

If you eval the response text it should give an array:

GM_xmlhttpRequest values:

http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/1_8081.png?cid=129373&key=b349856c835e4e849e6e5ecbf6e5e54e
http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/2_1555.png?cid=129373&key=b349856c835e4e849e6e5ecbf6e5e54e

XMLHttpRequest values:

http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/1_8081.png?cid=129373&key=dc6cdb99e41f6b698ff5a0ae1305a1db
http://manhua1021.104-250-142-226.cdndm5.com/11/10410/129373/2_1555.png?cid=129373&key=dc6cdb99e41f6b698ff5a0ae1305a1db

notice how the "key" part of the strings are different.

Also if I use the GM_xmlhttpRequest method, it doesn't show up in the network tab.

woxxomMod
§
Posted: 09-11-2015

it doesn't show up in the network tab.

Use Fiddler.

§
Posted: 10-11-2015
Edited: 10-11-2015

I found the problem. No referer header is being sent. It seems that by default it GM_xmlhttpRequest doesn't send a referer header so I needed to specify.

Ty guys. Still don't get why I had to use fiddler just to see this.

Post reply

Sign in to post a reply.