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.
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.
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.
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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With