Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Q&A Mod
- // @version 0.4a
- // @description //
- // @match http*://*.9tut.com/*
- // @match http*://*.9tut.net/*
- // @match http*://*.digitaltut.com/*
- // @match http*://*.certprepare.com/*
- // @match http*://*.networktut.com/*
- // @match http*://*.voicetut.com/*
- // @match http*://*.securitytut.com/*
- // @match http*://*.wirelesstut.com/*
- // @match http*://*.dstut.com/*
- // @match http*://*.rstut.com/*
- // @copyright Public Domain
- // ==/UserScript==
- var correctAns = [];
- formatQASection("ccnacorrectanswers", "Answer");
- formatQASection("ccnaquestionsnumber", "Question");
- formatQASection("ccnaexplanation", "Explanation");
- function findClass(node) {
- if((node.className == "ccnaexplanation") || (node.className == "ccnaquestionsnumber") || (node.className == "ccnacorrectanswers")) {
- if(node.innerHTML.trim() != "")
- return true;
- }
- var result = false;
- if(node.childNodes.length > 0) {
- var i;
- for(i=0; i < node.childNodes.length; i++) {
- result = findClass(node.childNodes[i]);
- if(result)
- break;
- }
- }
- return result;
- }
- function formatQASection(className, secName) {
- var c = document.getElementsByClassName("post");
- var x = c[0].getElementsByClassName(className);
- var qaCount = 0;
- for (i = 0; i < x.length; i++) {
- if(x[i].innerHTML.trim() == "") continue;
- var newNodeFix = x[i];
- while(newNodeFix.parentNode) {
- if(newNodeFix.parentNode.className != "content" && newNodeFix.parentNode.tagName != "DIV")
- newNodeFix = newNodeFix.parentNode;
- else
- break;
- }
- var newP = document.createElement("P");
- var newNode = null;
- var newNode2 = null;
- if(className == "ccnaquestionsnumber") {
- newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix.nextSibling);
- var answers = [];
- var ansCount = -1;
- var str;
- var newAnsNode = null;
- if(correctAns[qaCount]) {
- while(newNode.nextSibling) {
- if(findClass(newNode.nextSibling)) {
- break;
- } else {
- newNode2 = newNode.appendChild(newNode.nextSibling);
- if(newNode2.nodeType == 1) {
- str = newNode2.innerText;
- var matches = str.match(/^\s*([A-K]\s*(\.|\-|\–|\—)).*(?!\?){1}/gm);
- if(matches) {
- if(matches.length >= 1) {
- var lines = str.split(/\n/);
- for(l=0;l< lines.length;l++) {
- if(/^(\s*)(([A-K]\s*(\.|\-|\–|\—)){1}.*)/.test(lines[l])) {
- ansCount++;
- answers[ansCount] = lines[l].trim();
- if(!newAnsNode) {
- var newAnsP = document.createElement("P");
- newAnsNode = newNode.appendChild(newAnsP);
- }
- } else {
- if(ansCount < 0) {
- var newQuestP = document.createElement("P");
- newQuestP.innerHTML = lines[l];
- newNode.appendChild(newQuestP);
- continue;
- }
- str = lines[l].trim();
- if(str != "")
- answers[ansCount] += str + "<br>";
- }
- }
- newNode.removeChild(newNode2);
- }
- }
- }
- }
- }
- if(newAnsNode) {
- str = "<table>";
- if(correctAns[qaCount].length == 1) {
- for(a=0;a<answers.length;a++) {
- str += "<tr id=ansSelect" + (qaCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"radio\" name=\"ansRadio" + (qaCount+1) + "\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
- }
- } else {
- for(a=0;a<answers.length;a++) {
- str += "<tr id=ansSelect" + (qaCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"checkbox\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
- }
- }
- str += "</table>\n"
- newAnsNode.innerHTML = str;
- } else
- qaCount--;
- }
- } else {
- newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix);
- var newDiv = document.createElement ("DIV");
- newDiv.style.display = "none";
- var newButton = document.createElement("INPUT");
- newButton.setAttribute("type", "button");
- newButton.value = "Show "+secName;
- if(className == "ccnacorrectanswers") {
- var ansCorrect = newNodeFix.innerText.match(/[A-K](?!\w)/gm);
- if(ansCorrect) {
- correctAns[qaCount] = ansCorrect;
- newButton.onclick = (function (qaCount, ansCorrect) {
- return function() {
- if(this.nextSibling.style.display != "none") {
- this.value = "Show "+secName;
- this.nextSibling.style.display = "none";
- if(ansCorrect) {
- for(x=0;x<ansCorrect.length;x++)
- document.getElementById("ansSelect" + (qaCount+1) + ansCorrect[x]).style.backgroundColor = "";
- }
- } else {
- this.value = "Hide "+secName;
- this.nextSibling.style.display = "block";
- if(ansCorrect) {
- for(x=0;x<ansCorrect.length;x++)
- document.getElementById("ansSelect" + (qaCount+1) + ansCorrect[x]).style.backgroundColor = "limegreen";
- }
- }
- }
- })(qaCount,ansCorrect);
- } else {
- newButton.onclick = function (){
- if(this.nextSibling.style.display != "none") {
- this.value = "Show "+secName;
- this.nextSibling.style.display = "none";
- } else {
- this.value = "Hide "+secName;
- this.nextSibling.style.display = "block";
- }
- return false;
- };
- qaCount--;
- }
- } else {
- newButton.onclick = function (){
- if(this.nextSibling.style.display != "none") {
- this.value = "Show "+secName;
- this.nextSibling.style.display = "none";
- } else {
- this.value = "Hide "+secName;
- this.nextSibling.style.display = "block";
- }
- return false;
- };
- }
- newNode.appendChild(newButton);
- newNode2 = newNode.appendChild(newDiv);
- newNode2.appendChild(newNodeFix);
- while(newNode.nextSibling) {
- if(findClass(newNode.nextSibling))
- break;
- else
- newNode2.appendChild(newNode.nextSibling);
- }
- }
- qaCount++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment