我怎样才能从GM

编程入门 行业动态 更新时间:2024-10-10 06:16:08
本文介绍了我怎样才能从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 =www.hyperium/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){$ b $如果(responseDetails.responseText.match(/< b>< b>无效顺序< \ / td>< \ tr>< tr>< t>< #AAAA77 face = verdana,arial size = 2>目标行星正在阻挡所有渗透[\s\S]< BR> / im)){ //成功匹配 infiltrationResult ='Invalid Order'; } else { //匹配失败 infiltrationResult ='渗透成功创建'; } } });

当我添加

alert(infiltrationResult);

在分配后,我正确地看到了字符串。

但是,在函数退出后,我尝试了相同的提示,并得到:

undefined

任何想法我做错了什么?

解决方案

请求异步运行。这就是为什么函数首先需要一个 onload 回调函数。如果它是同步的,那么 GM_xmlhttpRequest 只会像普通函数那样返回响应细节。

在等待请求返回,调用 GM_xmlhttpRequest 之后的代码继续运行。您的脚本正确地识别出 infiltrationResult 未定义,因为请求尚未完成。

如果您需要做不仅仅是在请求返回时分配变量,然后在 onload 回调中执行。

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 = "www.hyperiums/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?

解决方案

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.

更多推荐

我怎样才能从GM

本文发布于:2023-11-29 13:30:01,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!