Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created January 24, 2012 03:52
Show Gist options
  • Save aya-eiya/1667691 to your computer and use it in GitHub Desktop.
Save aya-eiya/1667691 to your computer and use it in GitHub Desktop.
YahooのTOEICデイリーミニテスト(リーディング)から20問のランダム出題でテスト用紙を作成します。
// ==UserScript==
// @name YahooToeicTestSummarize
// @namespace jp.aya.eiya
// @description YahooのTOEICデイリーミニテスト(リーディング)から20問のランダム出題でテスト用紙を作成します。最初の問題を表示したときに、Summarizeするか聞かれます。OKすると、問題用紙を作成します。
// @include http://stepup.yahoo.co.jp/english/toeictest/question.html?c=5&c1=1
// ==/UserScript==
(function (){
Array.prototype.shuffle = function(){
var l = this.length;
var tmp = this.concat();
var lst = [];
while(l) lst.push(tmp.splice(Math.floor(Math.random()*l--),1));
return lst
}
var NAMESPACE='jp.aya.eiya.YahooToeicTestSummarize'
var req = new XMLHttpRequest();
var QUESTION_1ST = 365;
var QUESTION_MAX = 202;
var QUESTION_NUM = 20;
var qnum = 0;
var QS = new Array();
var dummyElem = document.createElement("div");
var qnums = new Array();
var qnum = 0;
var strage = (localStorage
&&
localStorage.getItem(NAMESPACE)) ?
eval(localStorage.getItem(NAMESPACE)):
{
STRAGE:true,
QUESIONS:{STRAGE:true},
SCORES:{STRAGE:true}
};
strage.save = function(){
var i=0;
var dig=function (obj){
var key="";
i++;if(i > 1000){return;}
var str = "{";
for(key in obj){
var _key = (typeof key == 'boolean' || typeof key == 'number')?key:'"'+(key.replace(/\\/g,"\\\\").replace(/"/g,"\\\""))+'"';
if(obj[key].STRAGE){
str += _key+':'+dig(obj[key])+',';
}else if(typeof obj[key] == 'boolean' || typeof obj[key] == 'number'){
str += _key+':'+obj[key]+',';
}else if(typeof obj[key] != 'function'){
str += _key+':"'+((obj[key]+"").replace(/\\/g,"\\\\").replace(/"/g,"\\\""))+'",';
}else{
// others like function, do nothing...
}
}
return str.replace(/,\s*$/,"}").replace(/\r/g,"\\r").replace(/\n/g,"\\n");
};
localStorage.setItem(NAMESPACE,"("+dig(this)+");");
};
strage.load = function(json_string){
var i=0;
var jsn=eval(json_string);
for(key in jsn){
this[key] = jsn[key];
}
};
(function (){
var i=0;
for(;i<QUESTION_MAX;i++) qnums[i] = QUESTION_1ST + i;
qnums = qnums.shuffle();
}
)();
if ('open' in req) {
function getPage() {
for(;strage.QUESIONS[qnums[qnum]];qnum++){
QS[QS.length] = strage.QUESIONS[qnums[qnum]];
if(QS.length >= QUESTION_NUM){
display();
strage.save();
return;
}
}
req.open ('GET', '/english/toeictest/answer.html?no=' + qnums[qnum], false);
req.onreadystatechange = processResponse;
req.send(null);
}
} else {
throw new Error ('[Error] XMLHttpRequest is not initializable.');
}
function processResponse(){
if(req.readyState == 4){
if(req.status == 200 || req.status == 201){
dummyElem.innerHTML = req.responseText;
getQustion(dummyElem);
setTimeout(getPage(),800);
} else {
alert("error");
}
}
}
function display(){
var i=0;
var bgcolor="";
var head = '<h1 style="font-size:28pt;text-align:left;">Yahoo Test Summary</h1>';
var footer = '<br><br><table border="0" cellpadding="0" cellspacing="0" style="border:3px solid black;" id="footer">'
+ '<tr><th colspan="2" style="background-color:#ccffff;border-bottom:1px solid black;height:32px;width:129px;"><input type="button" value="Score" /></th></tr>'
+ '<tr><td style="border-right:1px dotted black;width:64px;height:32px;font-size:20pt;">&nbsp;</td><td style="font-size:20pt;">&nbsp;</td></tr></table><br><br><br><br><br><br>';
var table = '<table border="0" cellpadding="0" cellspacing="0" style="border:3px solid black;">'
+'<tr><th colspan="1" rowspan="2" style="text-align:center;border-right:1px solid black;border-bottom:1px solid black;width: 12px;background-color:#cccccc;">No</th>'
+'<th colspan="1" rowspan="2" style="text-align:center;border-right:1px solid black;border-bottom:1px solid black;width:320px;background-color:#cccccc;">Question</th>'
+'<th colspan="4" rowspan="1" style="text-align:center;border-right:3px double black;border-bottom:1px solid black;width:485px;background-color:#cccccc;">Option</th>'
+'<th colspan="1" rowspan="2" style="text-align:center;border-right:3px solid black;border-bottom:1px solid black;width: 60px;background-color:#cccccc;">Answer</th>'
+'<th colspan="2" rowspan="1" style="text-align:center;border-right:3px solid black;border-bottom:1px solid black;width:400px;background-color:#cccccc;">Correct Answer</th></tr>'
+'<tr><th style="text-align:center;border-right:1px dotted black;border-bottom:1px solid black;width:120px;background-color:#cccccc;">A</th>'
+'<th style="text-align:center;border-right:1px dotted black;border-bottom:1px solid black;width:120px;background-color:#cccccc;">B</th>'
+'<th style="text-align:center;border-right:1px dotted black;border-bottom:1px solid black;width:120px;background-color:#cccccc;">C</th>'
+'<th style="text-align:center;border-right:3px double black;border-bottom:1px solid black;width:120px;background-color:#cccccc;">D</th>'
+'<th style="text-align:center;border-right:1px solid black;border-bottom:1px solid black;width: 60px;background-color:#cccccc;">Correct</th>'
+'<th style="text-align:center;border-right:3px solid black;border-bottom:1px solid black;width:340px;background-color:#cccccc;">Explanation</th></tr>';
var q=null;
for(;i < QS.length;i++){
q = QS[i];
bgcolor=(i % 2)?"#ffffdd":"#ccffff";
table += '<tr>' +
'<td class="QSumNO" style="text-align:center;border-right:1px solid black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;height:18px;">' + (i+1) + '</td>' +
'<td class="QSumQS" style="text-align:left; border-right:1px solid black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;padding:3px;">' + q.Question + '</td>' +
'<td class="QSumOA" style="text-align:center;border-right:1px dotted black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;">' + q.Options.A + '</td>' +
'<td class="QSumOB" style="text-align:center;border-right:1px dotted black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;">' + q.Options.B + '</td>' +
'<td class="QSumOC" style="text-align:center;border-right:1px dotted black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;">' + q.Options.C + '</td>' +
'<td class="QSumOD" style="text-align:center;border-right:3px double black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;">' + q.Options.D + '</td>' +
'<td class="QSumAS" style="text-align:center;border-right:3px solid black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:black;">' + createAnswerSelection(q.QNumber,q.CorrectAnswer ) + '</td>' +
'<td class="QSumCA" style="text-align:center;border-right:1px solid black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:' + bgcolor + ';">' + q.CorrectAnswer + '</td>' +
'<td class="QSumEP" style="text-align:left; border-right:3px solid black;border-bottom:1px solid black;background-color:' + bgcolor + ';color:' + bgcolor + ';">' + q.Explanation + '</td>' +
'</tr>';
}
table += '</table>';
document.body.innerHTML = head + table + footer;
document.getElementsByTagName('input')[0].onclick= function (){
var ss = document.getElementsByTagName('select');
var i=0;
var j=0;
var k=0;
var ops = null;
var nd = null;
var qs = null;
var ca = null;
var ep = null;
for(;i < ss.length;i++){
nd=ss[i];
qs = nd.parentNode.parentNode.getElementsByClassName('QSumQS')[0];
ca = nd.parentNode.parentNode.getElementsByClassName('QSumCA')[0];
ep = nd.parentNode.parentNode.getElementsByClassName('QSumEP')[0];
if(nd.value==1){
nd.style.backgroundColor = "#5555ff";
ca.style.color = ep.style.color = "black";
j++;
}else{
nd.style.backgroundColor = "#ff5555";
ca.style.color = ep.style.color = "red";
}
ops = nd.getElementsByTagName('option');
for(k=0;k<ops.length;k++){
nd=ops[k];
if(nd.value==1){
qs.innerHTML = qs.innerHTML.replace( /_{7}/, '<span style="color:'+ca.style.color+';font-weight:bold;text-decoration:underline;">'+QS[i].Options[nd.text] +'</span>');
console.log(qs.innerHTML);
nd.style.backgroundColor = "#5555ff";
}else{
nd.style.backgroundColor = "#ff5555";
}
}
}
var tbl = document.getElementsByTagName('table')[1];
tbl.getElementsByTagName('td')[0].innerHTML = j+'/'+i;
tbl.getElementsByTagName('td')[1].innerHTML = Math.ceil( j*100 / i ) + " %";
}
}
function createAnswerSelection(no,a){
return '<select id="Q'+no+'">'
+'<option style="background-color:#cccccc" value="-">-</option>'
+((a == 'A')?'<option value="1">A</option>':'<option value="0">A</option>')
+((a == 'B')?'<option value="1">B</option>':'<option value="0">B</option>')
+((a == 'C')?'<option value="1">C</option>':'<option value="0">C</option>')
+((a == 'D')?'<option value="1">D</option>':'<option value="0">D</option>')
+'</select>';
}
function getQustion(res){
var ns1 = res.getElementsByTagName('dl');
var ns2 = null;
var n1 = null;
var n2 = null;
var opt = null;
var i=0
var j=0;
var tmp = {
STRAGE :true,
QNumber : qnums[qnum],
Question : "",
Options :{STRAGE:true,A:"",B:"",C:"",D:""},
CorrectAnswer: "",
Explanation : ""
};
for(i=0;i<ns1.length;i++){
n1 = ns1[i];
if(!tmp.Question && n1.lang == 'en'){
tmp.Question = n1.getElementsByTagName('dt')[0].innerHTML;
ns2 = n1.getElementsByTagName('li');
for(j=0;j<ns2.length;j++){
n2 = ns2[j];
if(n2.innerHTML && n2.innerHTML.match(/^\([ABCD]\)/)){
opt = n2.innerHTML.match(/^\([ABCD]\)/)[0].charAt(1);
tmp.Options[opt] = n2.innerHTML.replace(/^\([ABCD]\)/,"");
}
}
}
if(tmp.Question && n1.id=='a-match'){
tmp.CorrectAnswer = n1.getElementsByTagName('span')[1].innerHTML;
}
if(tmp.CorrectAnswer && !tmp.Explanation && n1.parentNode.id=='a-sentence'){
n2 = n1.parentNode.getElementsByTagName('dd');
tmp.Explanation += "[日本語訳]<br>" + (n2[0].innerHTML.replace(/<[^>]+>/g,'')) + "<br>";
tmp.Explanation += "[解説]<br>" + (n2[1].innerHTML.replace(/<[^>]+>/g,''));
tmp.Explanation += '<div style="text-align:right;font-size:8pt"><a target="yahoo_toeic_sum" href="/english/toeictest/answer.html?no=' + tmp.QNumber +'" >解説ページ</a></div>';
}
}
strage.QUESIONS[tmp.QNumber] = tmp;
}
if(confirm("Summarize?")){
getPage();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment