Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Q&A Mod
- // @version 0.7
- // @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 = [];
- var availableAns = [];
- var qaAnsCount = 0;
- var qaQuestCount = 0;
- processDoc();
- function checkClass(node) {
- if(node.className != "content") {
- if(node.innerText) {
- if(node.innerText.trim() != "") {
- if(node.className == "ccnaquestionsnumber")
- return "Question";
- if(node.className == "ccnacorrectanswers")
- return "Answer";
- if(node.className == "ccnaexplanation")
- return "Explanation";
- if(/^Answer\:\s*/.test(node.innerText.trim()))
- return "Answer";
- if(/^Question\s+\d+/.test(node.innerText.trim()))
- return "Question";
- }
- }
- }
- return false;
- }
- function processDoc() {
- var post = document.getElementsByClassName("post")[0];
- var content = post.getElementsByClassName("content")[0];
- findSec("Question", content);
- }
- function findSec(secName, node) {
- var result = checkClass(node);
- if(result == secName) {
- formatQASection(secName, node);
- return result;
- }
- if(node.hasChildNodes()) {
- var i;
- for(i=0; i < node.childNodes.length; i++) {
- result = findSec(secName, node.childNodes[i]);
- }
- }
- return result;
- }
- function findClass(node) {
- var result = false;
- result = checkClass(node)
- if(result)
- return result;
- if(node.hasChildNodes()) {
- var i;
- for(i=0; i < node.childNodes.length; i++) {
- result = findClass(node.childNodes[i]);
- if(result)
- break;
- }
- }
- return result;
- }
- function formatQASection(secName, secNode) {
- var newNodeFix = secNode;
- 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(secName == "Question") {
- newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix.nextSibling);
- var answers = [];
- var ansCount = -1;
- var str;
- var newAnsNode = null;
- while(newNode.nextSibling) {
- var result = findClass(newNode.nextSibling);
- if(result) {
- if(result == "Answer") {
- formatQASection(result, newNode.nextSibling);
- }
- break;
- } else {
- newNode.appendChild(newNode.nextSibling);
- }
- }
- if(newNode.hasChildNodes()) {
- var i;
- for(i=0; i < newNode.childNodes.length; i++) {
- newNode2 = newNode.childNodes[i];
- if(newNode2.nodeType == 1) {
- var appendToParent = "";
- str = newNode2.innerText;
- if((newNode2.tagName == "P") && (/^\s*([A-K](?!\w)).*$/gm.test(str))) {
- str = newNode2.outerHTML;
- var lines = str.split(/\n|<\/?p>|(<[^>]*>)/);
- var matchCount = 0;
- for(l=0;l< lines.length;l++) {
- if(lines[l]) {
- lines[l] = lines[l].trim();
- if(lines[l] != "") {
- if(/^\s*([A-K](?!\w)).*$/.test(lines[l])) {
- matchCount++;
- if(ansCount < 0) {
- if((matchCount == 1) && (lines[l].charAt(0) == "A")) {
- ansCount++;
- answers[ansCount] = lines[l];
- if(!newAnsNode) {
- var newAnsP = document.createElement("P");
- newAnsNode = newNode.insertBefore(newAnsP, newNode2);
- }
- } else {
- appendToParent += lines[l];
- }
- } else {
- if(matchCount == 1) {
- if((answers[ansCount].charCodeAt(0)+1) == lines[l].charCodeAt(0)) {
- ansCount++;
- answers[ansCount] = lines[l];
- } else {
- if((lines[l].charAt(0) == "A") && (answers[ansCount].charAt(0) == "A")) {
- appendToParent += answers[ansCount];
- answers[ansCount] = lines[l];
- } else {
- answers[ansCount] += "<br>\n" + lines[l];
- }
- }
- } else {
- if(((answers[0].charCodeAt(0)+matchCount-1) == lines[l].charCodeAt(0)) && ((answers[ansCount].charCodeAt(0)+1) == lines[l].charCodeAt(0))) {
- ansCount++;
- answers[ansCount] = lines[l];
- } else {
- answers[ansCount] += "\n" + lines[l];
- }
- }
- }
- } else {
- if(ansCount < 0) {
- appendToParent += lines[l];
- continue;
- } else {
- answers[ansCount] += "\n" + lines[l];
- }
- }
- }
- }
- }
- } else {
- if(ansCount < 0) {
- appendToParent += newNode2.outerHTML;
- } else {
- if(newNode2.innerHTML.trim().replace(/( )+$/, "") != "") {
- str = newNode2.outerHTML;
- answers[ansCount] += "\n" + str;
- }
- }
- }
- if(appendToParent != "") {
- var newQuestP = document.createElement("P");
- newQuestP.innerHTML = appendToParent;
- if(newAnsNode) {
- newNode.insertBefore(newQuestP, newAnsNode);
- i++;
- } else
- newNode.insertBefore(newQuestP, newNode2);
- }
- newNode.removeChild(newNode2);
- }
- }
- }
- if(newAnsNode) {
- if(answers.length > 1) {
- availableAns[qaQuestCount] = [];
- str = "<table>\n";
- if(correctAns[qaQuestCount]) {
- if(correctAns[qaQuestCount].length == 1) {
- for(a=0;a<answers.length;a++) {
- availableAns[qaQuestCount][a] = answers[a].charAt(0);
- str += "<tr id=ansSelect" + (qaQuestCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"radio\" name=\"ansRadio" + (qaQuestCount+1) + "\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
- }
- } else {
- for(a=0;a<answers.length;a++) {
- availableAns[qaQuestCount][a] = answers[a].charAt(0);
- str += "<tr id=ansSelect" + (qaQuestCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"checkbox\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
- }
- }
- } else {
- for(a=0;a<answers.length;a++) {
- availableAns[qaQuestCount][a] = answers[a].charAt(0);
- str += "<tr id=ansSelect" + (qaQuestCount+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;
- newAnsNode.normalize();
- qaQuestCount++;
- } else {
- var newQuestP = document.createElement("P");
- newQuestP.innerHTML = answers[0];
- newNode.insertBefore(newQuestP, newAnsNode);
- newNode.removeChild(newAnsNode);
- }
- } else {
- if(/^Question\s+\d+/.test(secNode.innerText.trim())) {
- availableAns[qaQuestCount] = [];
- if(qaAnsCount == qaQuestCount){
- correctAns.splice(qaQuestCount, 0, [])
- qaAnsCount++;
- }
- qaQuestCount++;
- }
- }
- } 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(secName == "Answer") {
- var ansCorrect = newNodeFix.innerText.match(/[A-K](?!\w)/gm);
- if(ansCorrect) {
- correctAns[qaAnsCount] = ansCorrect;
- newButton.onclick = (function (qaAnsCount, ansCorrect) {
- return function() {
- var choices = availableAns[qaAnsCount];
- if(this.nextSibling.nextSibling.style.display != "none") {
- this.value = "Show "+secName;
- this.nextSibling.nextSibling.style.display = "none";
- if(ansCorrect) {
- for(x=0;x<choices.length;x++) {
- var trElement = document.getElementById("ansSelect" + (qaAnsCount+1) + choices[x]);
- var inputElement = trElement.getElementsByTagName("INPUT")[0];
- inputElement.disabled = false;
- trElement.style.backgroundColor = "";
- }
- }
- this.nextSibling.innerText = "";
- this.nextSibling.style.display = "none";
- } else {
- this.value = "Hide "+secName;
- this.nextSibling.nextSibling.style.display = "block";
- if(ansCorrect) {
- var bCorrect = true;
- for(x=0;x<choices.length;x++) {
- var trElement = document.getElementById("ansSelect" + (qaAnsCount+1) + choices[x]);
- var inputElement = trElement.getElementsByTagName("INPUT")[0];
- inputElement.disabled = true;
- if(ansCorrect.indexOf(choices[x]) < 0) {
- if(inputElement.checked) {
- bCorrect = false;
- trElement.style.backgroundColor = "red";
- }
- } else {
- if(!inputElement.checked)
- bCorrect = false;
- }
- for(y=0;y<ansCorrect.length;y++) {
- if(choices[x] == ansCorrect[y])
- trElement.style.backgroundColor = "limegreen";
- }
- }
- if(bCorrect) {
- this.nextSibling.style.color = "green";
- this.nextSibling.innerText = "\tCORRECT";
- } else {
- this.nextSibling.style.color = "red";
- this.nextSibling.innerText = "\tINCORRECT";
- }
- this.nextSibling.style.display = "inline";
- }
- }
- }
- })(qaAnsCount,ansCorrect);
- qaAnsCount++;
- } else {
- newButton.onclick = function (){
- if(this.nextSibling.nextSibling.style.display != "none") {
- this.value = "Show "+secName;
- this.nextSibling.nextSibling.style.display = "none";
- } else {
- this.value = "Hide "+secName;
- this.nextSibling.nextSibling.style.display = "block";
- }
- return false;
- };
- if(/^Answer\:\s*/.test(secNode.innerText.trim()))
- qaAnsCount++;
- }
- } else {
- newButton.onclick = function (){
- if(this.nextSibling.nextSibling.style.display != "none") {
- this.value = "Show "+secName;
- this.nextSibling.nextSibling.style.display = "none";
- } else {
- this.value = "Hide "+secName;
- this.nextSibling.nextSibling.style.display = "block";
- }
- return false;
- };
- }
- newNode.appendChild(newButton);
- var newSpan = document.createElement("SPAN");
- newSpan.id = "ansSpanQ" + qaAnsCount;
- newSpan.style.display = "none";
- newSpan.style.fontWeight = "bold";
- newNode.appendChild(newSpan);
- newNode2 = newNode.appendChild(newDiv);
- newNode2.appendChild(newNodeFix);
- while(newNode.nextSibling) {
- var result = findClass(newNode.nextSibling);
- if(result) {
- if(result != "Question") {
- formatQASection(result, newNode.nextSibling);
- }
- break;
- } else {
- newNode2.appendChild(newNode.nextSibling);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment