gsbabil gsbabil - 1 year ago 92
Javascript Question

How to find the request-URL from inside Greasemonkey's GM_xmlhttpRequest "onload" callback in case of 302 redirect?

I have a

function setup as follows (simplified version) in my Greasemonkey script.

synchronous: false,
method: "HEAD",
url: "",
onload: function(response){console.debug(url);},

  • GM_xmlhttpReqeust
    is called in asynchronous mode in my code.

  • Once accessed,
    does a 302 redirect to

  • I would like to access the value of the original
    parameter (
    ) inside
    callback function.

  • As per
    can be found in

Could someone please point me to the proper Greasemonkey/JavaScript way?

Answer Source

The response passed to onload is an object with these key properties:

  • readyState
  • responseHeaders
  • responseText
  • status
  • statusText
  • finalUrl

You want finalUrl, you get it like:

GM_xmlhttpRequest ( {
    synchronous:    false,
    method:         "HEAD",
    url:            "",
    onload:         function (response) {
        console.debug (response.finalUrl);
} );

Update for revised/clarified question:

In order to get/know the originally requested URL, you must call GM_xmlhttpRequest() in a closure. Like so:

var origURL = "";

(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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download