How can I return a value from GM_xmlhttprequest?

Posted by GeoffreyF67 on Stack Overflow See other posts from Stack Overflow or by GeoffreyF67
Published on 2009-02-08T06:05:53Z Indexed on 2010/05/22 21:10 UTC
Read the original article Hit count: 303

I have this code here:

var infiltrationResult;

while(thisOption) {
    var trNode = document.createElement('tr');
    var tdNode = document.createElement('td');
    var hrefNode = document.createElement('a');

    infPlanetID = thisOption.getAttribute('value');

  var myURL = "http://www.hyperiums.com/servlet/Planetinf?securitylevel=90&newinfiltr=New+infiltration&planetid=" + PlanetID + "&infplanetid=" + infPlanetID;

    GM_xmlhttpRequest({
        method: 'GET',
        url: myURL,
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
            'Accept': 'application/atom+xml,application/xml,text/xml',
        },
        onload: function(responseDetails) {
    			if (responseDetails.responseText.match(/<b>Invalid order<\/td><\/tr><tr><td><BR><center><font color=#AAAA77 face=verdana,arial size=2>The target planet is blocking all infiltrations[\s\S]<BR><BR>/im)) {
    				// Successful match
    				infiltrationResult = 'Invalid Order';
    			} else {
    				// Match attempt failed
    				infiltrationResult = 'Infiltration Successfully Created';
    			}
        }
    });

When I add

alert(infiltrationResult);

right after it is assigned, I correctly see the string.

However, after the function has exited, I have try the same alert and I get:

undefined

Any ideas what I'm doing wrong? I'm guessing it's some simple silly javascript synxtax I'm missing here :)

G-Man

© Stack Overflow or respective owner

Related posts about variable

Related posts about greasemonkey