MediaWiki:Gadget-quizScorePost.js

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

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

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

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

    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 = '<b>Post your score: </b><small><label for="postername' + i + '">Nickname: </label><input type="text" disabled="disabled" size="7" name="postername" id="postername' + i + '" value="' + mw.config.get('wgUserName') + '" style="background-color: lightgrey;" > </input><label for="quizpostcomment' + i + '">Comment (optional) </label><input type="text" size="30" name="quizpostcomment" id="quizpostcomment' + i + '"></input></small><button type="button" name="postScore" value="Post Score" onclick="quizPostScore(' + i + ', \'' + postLink + '\')">Post Score</button>';
        }
        else {
            postLine.innerHTML = '<b>Post your score: </b><small><label for="postername' + i + '">Nickname (optional): </label><input type="text"  size="7" name="postername" id="postername' + i + '"> </input><label for="quizpostcomment' + i + '">Comment (optional) </label><input type="text" size="30" name="quizpostcomment" id="quizpostcomment' + i + '"></input></small><button type="button" name="postScore" value="Post Score" onclick="quizPostScore(' + i + ', \'' + postLink + '\')">Post Score</button>';
        }
    }

}


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(/[[user:bawolff|Bawolff]] [[user talk:Bawolff|☺]][[User:Bawolff/Sandbox|☻]] 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("<big><b>Your score has been posted. <a href='" + decodeURIComponent(postLink) + "' > View</a> other peoples scores</b></big>", "scorePost-success");
        clearInterval(postInterval);
    }
    postInterval = window.setInterval(post, 900);
        
}
$(quizAskPostScores);

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