MediaWiki:Gadget-quizScorePost.js

try { // containerize gadget, to protect other gadgets if this one goes wrong

//Give you an easy post score+Comment at end of quiz

/* Todo: /*extern hasClass */ function quizAskPostScores {
 * Fix support for anons
 * make it worked message replace comment box (and fix scroll at same time)
 * test on other browsers

var postClass = "quiz-postScores";

var spans = document.getElementsByTagName("span");

var scores = []; for (var j = 0;j < spans.length; j++) { if (hasClass(spans[j], postClass)) { scores[scores.length] = spans[j]; }   }     if (scores.length === 0) return false; for (var i = 0; i < scores.length;i++) { var postLink = encodeURIComponent(scores[i].firstChild); var postLine = scores[i].parentNode.parentNode; //span containing post score suggestion if (mw.config.get('wgUserName')) { postLine.innerHTML = 'Post your score:  Nickname:  Comment (optional)  Post Score '; }       else { postLine.innerHTML = 'Post your score:  Nickname (optional):  Comment (optional)  Post Score '; }   }

}

if (typeof new_element !== "function") { function new_element (name, attributes) { //Create element, and attributes, and optionally add to tree // call as new_element(element name to create(string), attributes of element (object), optionally element to append as a child to) if (name) { var elm = document.createElement(name); } else { return null; } //set attributes if (typeof(attributes) == "object") { for (var i in attributes) { elm.setAttribute(i, attributes[i]); } } else { return null; } //attach to tree (but only if third argument given) if (arguments[2]) { var appendState = arguments[2].appendChild(elm) if (!appendState) { throw new Error("JS Error (quizPostScore-new_element): Can not find element to append new element to. (element)"); } } return elm; } }

function quizPostScore(quizNumb, postLink) { var quiz = document.getElementById("quiz" + quizNumb); var docEnd = document.getElementById("footer"); var iframe = new_element("iframe", {src: decodeURIComponent(postLink), style: "width:0; height:0;", width: "0", height: "0", id: "quizPostScore" + quizNumb}, docEnd); var newLine = "* "; var quizSpans = quiz.getElementsByTagName("span"); for (var i = 0; i < quizSpans.length; i++) { if (hasClass(quizSpans[i], "total")) { var total = quizSpans[i].firstChild.nodeValue; continue; }       if (hasClass(quizSpans[i], "score")) { var score = quizSpans[i].firstChild.nodeValue; }   }    if (!score || !total) { //Will this work due to global undefined weirdness? var problem = new Error("Could not figure out what your score was. Please post manually at" + decodeURIComponent(postLink)); alert(problem); throw problem; }   newLine += "'''"; newLine += score; newLine += "'''"; newLine += "/"; newLine += total; newLine += " - "; var comment = document.getElementById("quizpostcomment" + quizNumb).value.replace(/Bawolff ☺☻ 09:01, 26 December 2007 (UTC)$/, ""); newLine += comment; newLine += " &mdash; "; newLine += "";

var postInterval var post = function { var editbox = iframe.contentDocument.getElementById("wpTextbox1"); if (!editbox) return false; editbox.value += newLine; //do i need to check for newline? iframe.contentDocument.getElementById("wpSummary").value += "Post score (via quizScorePost.js) "; iframe.contentDocument.getElementById("wpSummary").value += newLine; iframe.contentDocument.getElementById("editform").target = '_parent';

iframe.contentDocument.getElementById("editform").submit;

window.setTimeout(function {if (iframe.contentWindow.wgAction !== "view") {alert("This might not of worked. Check manually at" + decodeURIComponent(postLink));}}, 9000); //in case edit conflict, etc jsMsg(" Your score has been posted.  View other peoples scores ", "scorePost-success"); clearInterval(postInterval); }   postInterval = window.setInterval(post, 900); } $(quizAskPostScores);

} catch (e) { // containerize gadget, to protect other gadgets if this one goes wrong // ignore }