質問

ここにこのコードがあります:

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

追加するとき

アラート(infiltrationResult);

割り当てられた直後、文字列が正しく表示されます。

ただし、関数が終了した後、私は同じアラートを試みました。

undefined

私が間違っていることはありますか?

役に立ちましたか?

解決

リクエストは非同期に実行されます。そうです どうして 関数は次のとおりです 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);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top