2012-06-14 60 views
1

我想用Greasemonkey自動回答問題。我有問題和答案,但如何選擇它們?如何使用Greasemonkey自動選擇特定的單選按鈕?

這裏的HTML:

<div class="vito_form_q"> 
    <div class="vito_form_title_min_no"> 
     <span id="ContentPlaceHolder1_lblquestionNo">8</span> 
    </div> 
    <div class="vito_form_title_min"> 
     <span id="ContentPlaceHolder1_lblquestion">Which sport is not playing with the ball?</span> 
    </div> 
    <div class="clearfloat"></div> 
    <div class="meter orange nostripes"><span style="width: 100%"></span></div> 
    <div class="quest"> 
     <table id="rbAnswer" class="q1"> 
     <tr><td> 
      <input id="rbAnswer_0" type="radio" name="ctl00$ContentPlaceHolder1$rbAnswer" value="157"/><label for="rbAnswer_0">Handball</label> 
     </td></tr> 
     <tr><td> 
      <input id="rbAnswer_1" type="radio" name="ctl00$ContentPlaceHolder1$rbAnswer" value="158"/><label for="rbAnswer_1">Basketball</label> 
     </td></tr> 
     <tr><td> 
      <input id="rbAnswer_2" type="radio" name="ctl00$ContentPlaceHolder1$rbAnswer" value="159"/><label for="rbAnswer_2">Football</label> 
     </td></tr> 
     <tr><td> 
      <input id="rbAnswer_3" type="radio" name="ctl00$ContentPlaceHolder1$rbAnswer" value="160"/><label for="rbAnswer_3">Fencing</label> 
     </td></tr> 


例如,對於這樣的問題:

<span id="ContentPlaceHolder1_lblquestion">Which sport is not playing with the ball?</span> 

選擇答案:

<td> 
    <input id="rbAnswer_3" type="radio" name="ctl00$ContentPlaceHolder1$rbAnswer" value="160" /> 
    <label for="rbAnswer_3">Fencing</label> 

我該怎麼辦這與Greasemonkey?

回答

1

製作的問題和答案的數組,像這樣:

var answerKey = [ 
     { q: "sport is not playing with the ball", a: "Fencing" } 
    , { 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. 
]; 


,這一行添加到您的Greasemonkey腳本the Metadata Block

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


然後,使用jQuery的超棒力量,您的腳本可以檢查正確的單選按鈕用下面的代碼:

See a demo of the underlying code at jsFiddle.

var answerKey = [ 
     { q: "sport is not playing with the ball", a: "Fencing" } 
    , { 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 = $("div div.vito_form_title_min span[id$='question']"); 

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  = $(this).parent(). nextAll ("div.quest").find ("label"); 

     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; 
} 
+0

你是完美的布魯克·亞當斯。今天我會和我的朋友一起嘗試,因爲它看起來很難。非常感謝你。 – Nikola

+0

嘿兄弟它的工作,但是當我在我的瀏覽器上嘗試它,這是行不通的。我想我沒有什麼問題。你能看看嗎? http://jsfiddle.net/kDThC/感謝:) – Nikola

+0

請參閱http://jsfiddle.net/kDThC/2/。有兩個問題; (1)該頁面使用與此處發佈的問題不同的ID。特別是,'ContentPlaceHolder1_lblSoru'與'ContentPlaceHolder1_lblquestion'。 (2)問題字符串不能有一些Unicode字符。在這種情況下,口音(')。在這種情況下,像我一樣用一段時間('.')替換它。 q值用於構建正則表達式。 –

相關問題