Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

javascript - How can I automatically select specific radio buttons with Greasemonkey?

I want to automatically answer questions with Greasemonkey. I have the questions and answers, but how can select them?

Here's the 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>


For example, for this question:

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

Select the answer:

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

How can I do this with Greasemonkey?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make an array of questions and answers, like so:

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.
];


Then add this line to the Metadata Block of your Greasemonkey script:

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


Then, using the awesome power of jQuery, your script can check the right radio buttons with the following code:

(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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...