I know that is a stupid question but i want to ask how i can save content to an array inside a xmlhttpRequest and use them after.
onlinestatus = new Array(count);
for (var i = 0; i <= count; i++) {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.website.com/player/' + player[i] + '/details.php',
onload: function(responseDetails) {
buddypage = jQuery(responseDetails.responseText);
online = jQuery("span#OnlineStatus", buddypage);
onlinestatus[i] = online.text();
}
});
GM_log("Current: " + onlinestatus[i]);
}
Two things will stop this working: the web request will be asynchronous, and the values will all be stored in the same location in the array.
Here's the order things will happen, assuming count = 2:
i = 0
i < count
, is true, continue for loopresult[0]
i = 1
i < count
, is true, continue for loopresult[1]
i = 2
i < count
, is false, exit for loopresult[i]
, so set result[2] = "foo"
result[i]
, so set result[2] = "bar"
That means that the result array ends up as [undefined, undefined, "bar"]
. The last value of the array could be foo or bar, depending on which web request finishes first. Either way, by the time the web requests complete, i
will have already been set to the wrong value, and the results will have already been logged, so it will look like the results array is empty.