使用Greasemonkey编译一系列单选按钮?

时间:2011-07-27 21:52:04

标签: javascript jquery forms greasemonkey

我必须按照以下顺序编辑一张表格,如下图所示:

  1. 第一个问题A
  2. 第二个问题D
  3. 第三个问题B
  4. 第四个问题
  5. 第五个问题D
  6. 使用Greasemonkey,但到目前为止我还没有在网上找到如何:(
    你能帮帮我吗?

    注意:现在看来OP在使用“Compile”时意味着“完整”。这给出了一个非常不同的问题!我已经要求OP使用Greasemonkey打开一个新问题 - 关于 在第三方网站上完成 表单。 :)


    enter image description here

1 个答案:

答案 0 :(得分:0)

你的问题来自哪里?

对于 问题,我们会将它们硬编码到Greasemonkey脚本中。 实际上,您可能希望从服务器使用GM xmlhttpRequest获取它们。

鉴于此数据:

var QandA   = [ { question: "Question 1",
                  answers:  ["Answer 1", "Answer 2"],
                  correct:  [2]
                },
                { question: "Isn't life without geometry pointless?",
                  answers:  ["Yes", "Maybe", "That's Squaresville, Daddy-o."],
                  correct:  [2,3]
                }
            ];

然后此代码将在页面中插入一个测验。 :
See it in action at jsFiddle.

//--- Build the Q&A HTML, with jQuery.
var htmlSrc     = '';

//--- Process one question at a time.
$.each (QandA, function (questIdx, questionObj) {
    var questNum    = questIdx + 1;
    htmlSrc    += '<h2><span class="GM_QuestNum">' + questNum + '</span>' + questionObj.question
                + '<span class="GM_Result">&nbsp;</span></h2>'
                + '<ul correctAns="' + questionObj.correct + '">';

    //--- Process one answer at a time.
    $.each (questionObj.answers, function (ansIdx, ansText) {
        var ansNum  = ansIdx + 1;
        htmlSrc    += '<li><label>'
                    + '<input type="radio" value="' + ansNum + '" name="GM_Quest_' + questIdx + '">'
                    + ansText
                    + '</label></li>'
                    ;
    } );
    htmlSrc    += '</ul>';
} );

//--- Generate the container and populate it with our question HTML.
$('body').append (      '<div id="GM_OurCustomQuiz">'
                    +   '<h1>Enjoy our fun Quiz!</h1>'
                    +   htmlSrc
                    +   '<button id="GM_CustQuizScoreBtn">Check Answers</button>'
                    +   '</div>'
                 );

//--- Make the score button live.
$('#GM_CustQuizScoreBtn').click ( function () {
    //--- Loop through the questions and check the answers.
    $('#GM_OurCustomQuiz ul').each ( function () {
        var jThis       = $(this);  /*-- "this" is a special variable inside each()
                                        Here it refers to the <ul> node for this
                                        loop iteration.
                                    */
        var ansVal      = jThis.find ("input:radio:checked").val ();
        var correctAns  = jThis.attr ("correctAns");
        var resultBox   = jThis.prev ('h2').children ('.GM_Result');
        if ($.inArray (ansVal, correctAns) >= 0 )
            //-- Pass! (That code is the HTML checkmark.)
            resultBox.html ('&#10003;').removeClass ('GM_WrongAns');
        else
            resultBox.html ('X').addClass ('GM_WrongAns');
    } );
} );



将所有内容放在Greasemonkey脚本中,然后添加样式:

// ==UserScript==
// @name            _Quiz Display
// @include         http://www.google.com/*
// @include         http://stackoverflow.com/questions/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

//--- This is our quiz data.
var QandA   = [ { question: "Question 1",
                  answers:  ["Answer 1", "Answer 2"],
                  correct:  [2]
                },
                { question: "Isn't life without geometry pointless?",
                  answers:  ["Yes", "Maybe", "That's Squaresville, Daddy-o."],
                  correct:  [2,3]
                }
            ];


//--- Build the Q&A HTML, with jQuery.
var htmlSrc     = '';

//--- Process one question at a time.
$.each (QandA, function (questIdx, questionObj) {

    var questNum    = questIdx + 1;
    htmlSrc    += '<h2><span class="GM_QuestNum">' + questNum + '</span>' + questionObj.question
                + '<span class="GM_Result">&nbsp;</span></h2>'
                + '<ul correctAns="' + questionObj.correct + '">';

    //--- Process one answer at a time.
    $.each (questionObj.answers, function (ansIdx, ansText) {

        var ansNum  = ansIdx + 1;
        htmlSrc    += '<li><label>'
                    + '<input type="radio" value="' + ansNum + '" name="GM_Quest_' + questIdx + '">'
                    + ansText
                    + '</label></li>'
                    ;
    } );
    htmlSrc    += '</ul>';
} );


//--- Generate the container and populate it with our question HTML.
$('body').append (      '<div id="GM_OurCustomQuiz">'
                    +   '<h1>Enjoy our fun Quiz!</h1>'
                    +   htmlSrc
                    +   '<button id="GM_CustQuizScoreBtn">Check Answers</button>'
                    +   '</div>'
                 );


//--- Make the score button live.
$('#GM_CustQuizScoreBtn').click ( function () {

    //--- Loop through the questions and check the answers.
    $('#GM_OurCustomQuiz ul').each ( function () {
        var jThis       = $(this);  /*-- "this" is a special variable inside each()
                                        Here it refers to the <ul> node for this
                                        loop iteration.
                                    */
        var ansVal      = jThis.find ("input:radio:checked").val ();
        var correctAns  = jThis.attr ("correctAns");
        var resultBox   = jThis.prev ('h2').children ('.GM_Result');

        if ($.inArray (ansVal, correctAns) >= 0 )
            //-- Pass! (That code is the HTML checkmark.)
            resultBox.html ('&#10003;').removeClass ('GM_WrongAns');
        else
            resultBox.html ('X').addClass ('GM_WrongAns');
    } );
} );



//--- Use CSS styles to make everything look pretty.
GM_addStyle ( (<><![CDATA[
    #GM_OurCustomQuiz {
        color:                  black;
        background:             gold;
        text-align:             left;
        font-size:              16px;
        font-weight:            normal;
        font-family:            "Trebuchet MS", Helvetica, Tahoma, Arial, sans-serif;
        padding:                10px 5%;
        line-height:            1.5;
        max-width:              60%;
        max-height:             750px;
        opacity:                0.95;
        position:               absolute;
        padding:                0ex 3ex 1ex 3ex;
        top:                    0.5ex;
        left:                   0.5ex;
        overflow-x:             hidden;
        overflow-y:             auto;
        border:                 3px outset black;
    }
    #GM_OurCustomQuiz h1, h2 {
        line-height:            1.2;
        margin-bottom:          0.9em;
        font-weight:            bold;
    }
    #GM_OurCustomQuiz h1 {
        font-size:              2em;
        text-shadow:            0px 2px 3px #555;
    }
    #GM_OurCustomQuiz h2 {
        font-size:              1.2em;
    }
    #GM_OurCustomQuiz input[type=radio] {
        margin-right:           0.7ex;
    }
    #GM_OurCustomQuiz label {
        cursor:                 pointer;
    }
    #GM_OurCustomQuiz button {
        margin:                 1em;
        padding:                1ex;
        font-size:              120%;
        cursor:                 pointer;
        background:             buttonface;
        border:                 4px outset buttonface;
        border-radius:          8px 8px 8px 8px;
        box-shadow:             2px 2px 4px gray;
    }
    #GM_OurCustomQuiz button:hover {
        color:                  red;
        text-shadow:            0 0 3px #F66;
    }
    #GM_OurCustomQuiz ol, ul {
        list-style-type:        none;
        list-style-image:       none;
        list-style-position:    outside;
        padding-left:           3ex;
        margin-top:             -1ex;
    }
    .GM_QuestNum {
        margin-right:           1ex;
    }
    .GM_QuestNum:after {
        content:                ')';
    }
    .GM_Result {
        color:                  lime;
        display:                inline-block;
        font-family:            comic Sans MS;
        font-size:              4ex;
        font-weight:            bold;
        margin:                 0 0 0 6px;
        text-shadow:            2px 2px 5px;
        width:                  32px;
        height:                 44px;
        padding:                0;
    }
    .GM_WrongAns {
        color:                  red;
        font-weight:            normal;
    }
]]></>).toString () );