【问题标题】:How can I automatically select specific radio buttons with Greasemonkey?如何使用 Greasemonkey 自动选择特定的单选按钮?
【发布时间】:2012-06-17 00:34:45
【问题描述】:

我想用 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 做到这一点?

【问题讨论】:

    标签: javascript html radio-button 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;
    }
    

    【讨论】:

    • 你是完美的布洛克亚当斯。我今天会和我的朋友一起尝试,因为它看起来很难。非常感谢。
    • 嘿兄弟,它正在工作,但是当我在我的浏览器上尝试它时,它不起作用。我觉得我的问题不大。你能看看吗? jsfiddle.net/kDThC 谢谢 :)
    • jsfiddle.net/kDThC/2。有两个问题; (1) 该页面使用的 ID 与问题中此处发布的 ID 不同。具体来说,ContentPlaceHolder1_lblSoruContentPlaceHolder1_lblquestion。 (2) 问题字符串不能包含一些 Unicode 字符。在这种情况下,重音符号 ( ` )。在这种情况下,像我一样用句点 (.) 替换它。 q 值用于构建正则表达式。
    • 很抱歉问了很多问题。我做了我的代码,你可以在这里看到jsfiddle.net/kDThC/4它正在进行小提琴测试但是这些代码不适用于greasemonkey。
    • 恭喜!很高兴能提供帮助。
    猜你喜欢
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2022-01-10
    相关资源
    最近更新 更多