我GM_xmlhttpRequest
在这样的循环中运行:
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);
}
它工作正常,但问题是预期结果向下移动了一行:
entryName1
entryName2 result1
entryName3 result2
entryName4 result3
... ...
因此,正如我所见,在下一次循环迭代中调用onload
my 的同步,但使用上一次迭代的参数。我该如何解决?GM_xmlhttpRequest
url