I have a GM_xmlhttpReqeust
function setup as follows (simplified version) in my Greasemonkey script.
GM_xmlhttpRequest({
synchronous: false,
method: "HEAD",
url: "http://www.example1.com",
onload: function(response){console.debug(url);},
});
GM_xmlhttpReqeust
is called in asynchronous mode in my code.
Once accessed, http://www.example1.com
does a 302 redirect to http://www.example2.com
I would like to access the value of the original url
parameter (http://www.example1.com
) inside onload
callback function.
As per GM_xmlhttpReqeust
documentation, http://www.example2.com
can be found in response.finalUrl
inside onload
callback.
Could someone please point me to the proper Greasemonkey/JavaScript way?
The response
passed to onload
is an object with these key properties:
You want finalUrl
, you get it like:
GM_xmlhttpRequest ( {
synchronous: false,
method: "HEAD",
url: "http://www.google.com",
onload: function (response) {
console.debug (response.finalUrl);
}
} );
In order to get/know the originally requested URL, you must call GM_xmlhttpRequest()
in a closure. Like so:
var origURL = "http://www.google.com";
(function (targURL) {
GM_xmlhttpRequest ( {
synchronous: false,
method: "HEAD",
url: targURL,
onload: function (response) {
console.log ("orig URL: ", targURL);
console.log ("final URL: ", response.finalUrl);
}
} );
} ) (origURL);