javascript - How can I adapt this previous script answer to my target pages? -
i want browser able highlight correct answers questions in green.
found an answer seems right idea (how can automatically select specific radio buttons greasemonkey?) don't know enough javascript use it.
my html code shown in this fiddle.
desired output example should this:
how adapt other answer situation?
the var questiontxt =
, var ansforthisq =
lines must adapted target page's specific html.
likewise, code inside ansforthisq.each (...
loop must tailored.
given sample html has structure:
<div class="soru-content"> <div class="point" style="margin-right:0px"> <span><img src="img_url"/></span> </div> <div class="soru"> <p>how many countries participate in games?</p> <ul> <li><a href="ans_url1"><span>12</span></a></li> <li><a href="ans_url2"><span>204</span></a></li> <li><a href="ans_url3"><span>552</span></a></li> <li><a href="ans_url4"><span>715</span></a></li> </ul> </div> </div>
then adapted script code be:
var answerkey = [ { q: "how many countries participate in games?", a: "204" } , { q: "when olympics start in london", a: "july 27" } // etc. ]; //--- loop through question(s) on page , answer if possible. var questiontxt = $("div.soru-content div.soru p"); questiontxt.each ( function () { var bfoundanswer = false; var answertxt = getanswer (this.textcontent); if (answertxt) { //--- have answer text, find matching radio button , select it. var ansforthisq = $(this).parent ().find ("ul span"); ansforthisq.each ( function () { var zregexp = new regexp (answertxt, 'i'); if (zregexp.test (this.textcontent) ) { bfoundanswer = true; $(this).css ("background", "lime"); return false; // end loop } } ); } else { alert ("i don't know how answer: '" + this.textcontent + "'"); $(this).css ("background", "pink"); } if ( answertxt && ! bfoundanswer) { alert ("the page not have specified answer question: '" + this.textcontent + "'"); $(this).css ("background", "pink"); } } ); function getanswer (questiontext) { (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; }
see the updated fiddle.
more information on jquery selectors can found in this part of doc.
Comments
Post a Comment