Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest without cookies

How can I send a request from javascript which would not use cookies? I want to do it from greasemonkey, so I don't care about same origin and can use both original xmlhttprequest or greamonkey's GM_xmlhttpRequest.

I need to fetch a page from the same website, but unauthentificated. Browser (Firefox) always sends all the cookies which FF have for that domain.

Background: I am working on a GM script which displays full size version of profile images. However the only way to know its URL, I must fetch the profile page for that user. This must be done unauthentificated, otherwise those users would be notified of me looking at their profile. Right now for development I use php on my server to fetch the profile page, but this is not scalable with the distribution of GM script for other users.

like image 386
Marki555 Avatar asked Apr 24 '15 13:04

Marki555


People also ask

Can XMLHttpRequest set cookie?

Note: XMLHttpRequest responses from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request, regardless of Access-Control- header values.

How do I bypass cookies on a server?

To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL.

What does XHR mean?

XMLHttpRequest (XHR) is a JavaScript API to create AJAX requests. Its methods provide the ability to send network requests between the browser and a server.


1 Answers

You can set the mozAnon or anonymous flag on the request options to suppress sending/storing cookies.

Unfortunately these flags aren't documented in the wiki yet, but they seem to be available since Version 3.8beta3 (April 18, 2016).

GM_xmlhttpRequest({
   method: 'GET',
   url: url,
   anonymous: true, // remove original cookies
   headers: {
      cookie: 'whatever' // add custom cookies if necessary
   },
   onload: function(res) {
      // optionally parse the reponse cookies which are otherwise lost
      const cookieRegex = /^Set-Cookie: (.*)$/gm;
      const cookies = [];
      while (cookieRegex.exec(res.responseHeaders)) {
         cookies.push(RegExp.$1);
      }
      console.log(cookies);
   }
});
like image 158
Robert Avatar answered Sep 25 '22 18:09

Robert