1

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?

gsbabil
  • 7,505
  • 3
  • 26
  • 28

3 Answers3

2

I got a stupid solution below, I hope it will work.

GM_xmlhttpRequest ( {
    synchronous:    false,
    method:         "HEAD",
    url:            "http://www.google.com",
    onload:         function (response) {
        console.debug (this.url);
    }
} );
Zhou Lee
  • 96
  • 4
  • Hi @Zhou Lee, the doc is here http://wiki.greasespot.net/GM_xmlhttpRequest, but it's not obvious how to access the `url` from inside `onload` callback. – gsbabil Jan 10 '13 at 06:26
  • Close, but `url` would still not be defined inside `onload`. *However*, since your solution passes an object, it can use the `this` scope to access `url`. I took the liberty of fixing the answer for you, and it does work after my tweak. ... PS: this is just another way to do a closure. – Brock Adams Jan 10 '13 at 08:21
  • Huh. Yeah, you're right. Already upvoted. Although this is a bad habit to be in, as different functions/cases might override `this`. For this specific question, it should be OK. – Brock Adams Jan 10 '13 at 09:46
  • Thanks. I tested both yours and Brock's solutions and they both work. If I could accept two, I would have accepted your solution as correct as well. I have up-voted you and accepting @Brock's solution as correct for demonstrating the more accurate JavaScript way. Thanks again to you both. – gsbabil Jan 10 '13 at 10:39
2

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:            "http://www.google.com",
    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 = "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);
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Hi @Brock Adams, Actually I don't want the `finalUrl`. I'll change `http://www.google.com` to `http://www.example1.com`. `http://www.example1.com` does a 302 redirect to `http://www.example2.com` which is found in `finalUrl`. I would like to get the original request URL which is `http://www.example1.com` as set in `url` parameter to `GM_xmlhttpReqeust`. I am going to change the question for more clarity. Thanks for your response. – gsbabil Jan 10 '13 at 07:02
0

referring to

http://userscripts-mirror.org/topics/51161

Firstly you need:

var method = this;
  var oldargs = [].slice.call( arguments, 1 );
  return function () {
    var newargs = [].slice.call( arguments );
    return method.apply( thisObject, oldargs.concat( newargs ));
  };
}

Then you go, similar way...

enter image description here

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
pendave
  • 1
  • 2