Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 如何从GM_xmlhttprequest返回值?_Variables_Greasemonkey_Global Variables_Alert_Undefined - Fatal编程技术网

Variables 如何从GM_xmlhttprequest返回值?

Variables 如何从GM_xmlhttprequest返回值?,variables,greasemonkey,global-variables,alert,undefined,Variables,Greasemonkey,Global Variables,Alert,Undefined,我这里有这个代码: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var tdNode = document.createElement('td'); var hrefNode = document.createElement('a'); infPlanetID = thisOption.getAttribute('value');

我这里有这个代码:

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';
                }
        }
    });

知道我做错了什么吗?

请求异步运行。这就是为什么函数首先采用
onload
回调函数的原因。如果是同步的,
GM_xmlhttpRequest
将像普通函数一样简单地返回响应细节

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

如果您需要在请求返回时不仅仅分配变量,那么请在
onload
回调中执行此操作。

尝试以下操作:

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';
                        }
                        sentback(infiltrationResult);//Sent it when it loads only
        }
    });

function sentback(x){
    alert(x);
}
var渗透结果;
while(此选项){
var trNode=document.createElement('tr');
var tdNode=document.createElement('td');
var hrefNode=document.createElement('a');
infPlanetID=thispoption.getAttribute('value');
var myURL=”http://www.hyperiums.com/servlet/Planetinf?securitylevel=90&newinfiltr=New+渗透&planetid=“+planetid+”&infplanetid=“+infplanetid;
GMxmlHttpRequest({
方法:“GET”,
url:myURL,
标题:{
“用户代理”:“Mozilla/4.0(兼容)Greasemonkey”,
“接受”:“application/atom+xml,application/xml,text/xml”,
},
onload:函数(responseDetails){
if(responseDetails.responseText.match(/Invalid order
目标行星正在阻止所有渗透[\s\s]

/im)){ //成功的比赛 渗透结果='无效顺序'; }否则{ //匹配尝试失败 渗透结果='渗透成功创建'; } sentback(渗透结果);//仅在加载时发送 } }); 函数sentback(x){ 警报(x); }
你说得绝对正确。将与该结果有关的代码移到函数中修复了所有问题。谢谢!G-Man
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';
                        }
                        sentback(infiltrationResult);//Sent it when it loads only
        }
    });

function sentback(x){
    alert(x);
}