Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return a value from GM_xmlhttprequest?

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?

like image 329
GeoffreyF67 Avatar asked Feb 08 '09 06:02

GeoffreyF67


2 Answers

The request runs asynchronously. That's why the function takes an onload callback function in the first place. If it were synchronous, then GM_xmlhttpRequest would simply return the response details like an ordinary function.

While waiting for the request to return, the code after the call to GM_xmlhttpRequest continues running. Your script correctly identifies that infiltrationResult is undefined because the request hasn't completed yet.

If you need to do more than just assign the variable when the request comes back, then do that in the onload callback.

like image 142
Rob Kennedy Avatar answered Nov 15 '22 10:11

Rob Kennedy


Try this:

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';
                        }
                        sentback(infiltrationResult);//Sent it when it loads only
        }
    });

function sentback(x){
    alert(x);
}
like image 22
DarkThanos Avatar answered Nov 15 '22 10:11

DarkThanos