1

I'm running GM_xmlhttpRequest in a loop like this:

for (var i = 0; i < entries.length; i++) {
    var entryName = entries[i];
    var requestResult = '';
    GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://example.net/api/search.xml?q=' + entryName,
        synchronous: true,
        onload: function (response) {
            if (response.status == 200) {
                responseXML = new DOMParser().parseFromString(response.responseText, "text/xml");
                requestResult = responseXML.evaluate('//entry[id/text() = "' + entryIndex + '"]/start_date/text()',
                        responseXML, null, XPathResult.STRING_TYPE, null).stringValue;
            } else {
                requestResult = 'request error';
            }
        }
    });
    console.log(entryName + ' ' + requestResult);
}

It works okay but the problem is that expected results are shifted one row down:

entryName1
entryName2  result1
entryName3  result2
entryName4  result3
...         ...

So as I see it synchronous onload of my GM_xmlhttpRequest is called on the next loop iteration but with url parameter from previous iteration. How do I fix this?

ttaaoossuuuu
  • 7,786
  • 3
  • 28
  • 58

0 Answers0