我如何使用Greasemonkey自动选择特定的单选按钮? [英] How can I automatically select specific radio buttons with 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>哪一项运动不是在玩球?< / span>
< / div>
< div class =clearfloat>< / div>
< div class =meter orange nostripes>< span style =width:100%>< / span>< / div>
< div class =quest>
< table id =rbAnswerclass =q1>
< tr>< td>
< input id =rbAnswer_0type =radioname =ctl00 $ ContentPlaceHolder1 $ rbAnswervalue =157/>< label for =rbAnswer_0>手球< / label>
< / td>< / tr>
< tr>< td>
< input id =rbAnswer_1type =radioname =ctl00 $ ContentPlaceHolder1 $ rbAnswervalue =158/>< label for =rbAnswer_1> Basketball< / label>
< / td>< / tr>
< tr>< td>
< input id =rbAnswer_2type =radioname =ctl00 $ ContentPlaceHolder1 $ rbAnswervalue =159/>< label for =rbAnswer_2> Football< / label>
< / td>< / tr>
< tr>< td>
< input id =rbAnswer_3type =radioname =ctl00 $ ContentPlaceHolder1 $ rbAnswervalue =160/>< label for =rbAnswer_3> Fencing< / label>
< / td>< / tr>
例如,对于这个问题:
< span id =ContentPlaceHolder1_lblquestion>哪一项运动不是在玩球?<跨度>
选择答案:
< TD>
< input id =rbAnswer_3type =radioname =ctl00 $ ContentPlaceHolder1 $ rbAnswervalue =160/>
< label for =rbAnswer_3>隔离栏< / label>
我如何使用Greasemonkey做到这一点?
制作一系列的问题和答案,如下所示:
var answerKey = [
{q:sport is not playing with the ball,a:击剑}
,{q:Best SciFi franchise,a:Star Trek }
,{q:最坏的怪物,a:Bun-Bun}
,{q:此列表中最有毒的化学物质,a:露水}
//等等
];
然后添加这个请转至Greasemonkey脚本的元数据块:
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
I want to automatically answer questions with Greasemonkey. I have the questions and answers, but how can select them? Here's the HTML: For example, for this question: Select the answer: How can I do this with Greasemonkey? Make an array of questions and answers, like so: Then add this line to the Metadata Block of your Greasemonkey script: (See a demo of the underlying code at jsFiddle.)
这篇关于我如何使用Greasemonkey自动选择特定的单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
然后,使用
var answerKey = [
{q:sport is not playing with the ball,a:Fencing}
,{q:Best SciFi特许经营,a:星际迷航}
,{q:最坏的怪物,a:Bun-Bun}
,{q:此列表中毒性最大的化学品, a:山露}
//等
];
// ---循环浏览页面上的问题并尽可能回答。
var questionTxt = $(div div.vito_form_title_min span [id $ ='question']);
questionTxt.each(function(){
var bFoundAnswer = false;
var answerTxt = getAnswer(this.textContent);
if(answerTxt){
// ---我们有答案文本,现在找到匹配的单选按钮并选择它
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);
返回false; //结束循环
}
});
}
else {
alert(我不知道如何回答:'+ this.textCont ent +');
$(this).css(background,pink);
}
if(answerTxt&!bFoundAnswer){
alert(该页面没有针对该问题的指定答案:'+ 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;
}
返回null;
}
<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>
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.
];
// @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: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;
}