Question

I am curious as to how GM_xmlhttpRequest() reads a page in certain situations.
Does GM_xmlhttpRequest access the website as if I am accessing the website in my browser?

Do sessions remain valid? For example: If I log into a website from one browser tab and then, from another tab, I send a GM_xmlhttpRequest from my script, is this new request also logged in?

One reason I ask is because at one point in my script I am sending a GM_xmlhttpRequest to one of my domain's pages that reports back the REMOTE_ADDR from PHP. This is reporting back my computer's IP (which is what I want) instead of the page I am calling this function from.

Was it helpful?

Solution 2

OMG it totally does!!!!!!

var server2="http://www.somesiteimloggedinto.com";
var test_data=function(){
    GM_xmlhttpRequest({
        method: "GET",
        url: server2,
        onload: function(response) {
            if(response.status == 200){
                alert(response.responseText);
            }
        },
        onerror: function(response) {
            console.log("Connection to "+server2+" failed.");
        }
    });
};
test_data();

OTHER TIPS

Does GM_xmlhttpRequest access the website as if I am accessing the website in my browser? As in, do sessions remain valid?

Yes and no. Normally, GM_xmlhttpRequest looks just like a Firefox request to the URL. It sends the usual headers and it normally sends any cookies. And, yes, the request is coming from your browser, so it reports your IP address. This means that any sessions you have with a web page are usually preserved.

However:

  1. If the URL is cross-domain to the webpage and you have disabled third-party cookies, then GM_xmlhttpRequest will NOT send cookies and the target site won't know about any session you have with it.

  2. GM_xmlhttpRequest loads only the requested URL. If it is a web page any: images, CSS files, JS files, etc. -- that the web page calls -- will not be loaded nor processed. Any embedded javascript in the page will not be run.

    See "How to get an AJAX get-request to wait for the page to be rendered before returning a response?" for more information and tips if you need to fetch an AJAX-powered site.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top