我必须刷新我的Greasemonkey脚本运行的页面吗?

时间:2013-10-23 22:47:34

标签: greasemonkey

我必须刷新页面才能运行我的脚本。我的代码需要做些什么才能让它无需刷新?

HTML

                    <div id="lblSoru" align="center" style="position: relative; top: 100px; font-size:20px; font-family: proxima_nova_rgregular; color:" class="font">Which country is bigger than another</div>     

        <td><input id="rdCevaplar_0" type="radio" name="rdCevaplar" value="1" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$0\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_0">France</label></td>
    </tr><tr>
        <td><input id="rdCevaplar_1" type="radio" name="rdCevaplar" value="2" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$1\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_1">Germany</label></td>
    </tr><tr>
        <td><input id="rdCevaplar_2" type="radio" name="rdCevaplar" value="3" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$2\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_2">Romania</label></td>
    </tr><tr>
        <td><input id="rdCevaplar_3" type="radio" name="rdCevaplar" value="4" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;rdCevaplar$3\&#39;,\&#39;\&#39;)&#39;, 0)" /><label for="rdCevaplar_3">Greece</label></td>
    </tr>

Greasemonkey代码:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

 var answerKey   = [
      { q: "Which country is bigger than another", a: "Germany" }
    , { q: "Best SciFi franchise", a: "Star Trek" }
    , { q: "Most badass monster", a: "Bun-Bun" }
    , { q: "Most toxic chemical in this list", a: "Mountain Dew" }
    // etc.
];

//--- Loop through the question(s) on the page and answer then if possible.
var questionTxt = $("#lblSoru");

questionTxt.each ( function () {
    var bFoundAnswer    = false;
    var answerTxt       = getAnswer (this.textContent);


    if (answerTxt) {
        //--- We have the answer text, now find the matching radio button and select it.
        var ansForThisQ     = $("label");
        console.log ("getAnswer: ", ansForThisQ);

        ansForThisQ.each ( function () {
            var zRegExp     = new RegExp (answerTxt, 'i');
            if (zRegExp.test (this.textContent) ) {
                bFoundAnswer    = true;
                var label       = $(this);
                var radioButt   = $("#" + label.prop ("for") );
                radioButt.prop  ("checked", "checked");
                label.css       ("background", "lime");
                return false;   // End loop
            }
        } );
    }
    else {
        alert ("I don't know how to answer: '" + this.textContent + "'");
        $(this).css ("background", "pink");
    }
    if ( answerTxt  &&  ! bFoundAnswer) {
        alert ("The page does not have the specified answer for the question: '" + this.textContent + "'");
        $(this).css ("background", "pink");
    }
} );

function getAnswer (questionText) {
    for (var J = answerKey.length - 1;  J >= 0;  --J) {
        var zRegExp = new RegExp (answerKey[J].q, 'i');

        if (zRegExp.test (questionText) )
            return answerKey[J].a;
    }
    return null;
}

1 个答案:

答案 0 :(得分:-1)

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

var answerKey = [
    { q: "Which country is bigger than another", a: "Germany" }, 
    { q: "Best SciFi franchise", a: "Star Trek" }, 
    { q: "Most badass monster", a: "Bun-Bun" }, 
    { q: "Most toxic chemical in this list", a: "Mountain Dew" }
];
var $lblSoru = $('#lblSoru');
var answerTxt = getAnswer($lblSoru.text());
if(answerTxt) {
    var bFoundAnswer = false;
    $('label').each(function() {
        if(!bFoundAnswer && $(this).text().toLowerCase() == answerTxt.toLowerCase()) {
            $('#' + $(this).attr('for')).attr('checked', 'checked');
            bFoundAnswer = true;
        }
    });
    if(!bFoundAnswer) {
        alert ("The page does not have the specified answer for the question: '" + $lblSoru.text() + "'");
        $lblSoru.css('background', 'pink');
    }
}
else {
    alert("I don't know how to answer: '" + $lblSoru.text() + "'");
    $lblSoru.css('background', 'pink');
}

function getAnswer(questionText) {
    for(var i = 0; i < answerKey.length; i++) {
        if(answerKey[i].q.toLowerCase() == questionText.toLowerCase()) {
            return answerKey[i].a;
        }
    }
    return null;
}

Working Demo

顺便说一下,<tr>之后你错过了Which country is bigger than another</div>