如何从 GM_xmlhttprequest 返回值?
我这里有这段代码:
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';
}
}
});
当我添加时
警报(渗透结果);
分配后,我正确地看到了该字符串。
但是,在该函数退出后,我尝试了相同的警报,然后得到:
undefined
你知道我做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该请求异步运行。 这就是该函数首先采用
onload
回调函数的原因。 如果它是同步的,那么GM_xmlhttpRequest
将像普通函数一样简单地返回响应详细信息。在等待请求返回时,调用
GM_xmlhttpRequest
之后的代码将继续运行。 您的脚本正确地识别出infiltrationResult
未定义,因为请求尚未完成。如果您需要做的不仅仅是在请求返回时分配变量,请在
onload
回调中执行此操作。The request runs asynchronously. That's why the function takes an
onload
callback function in the first place. If it were synchronous, thenGM_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 thatinfiltrationResult
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.尝试这个:
Try this: