Guest User

9tut Q&A Mod 0.7

a guest
Sep 8th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name       Q&A Mod
  3. // @version    0.7
  4. // @description  //
  5. // @match      http*://*.9tut.com/*
  6. // @match      http*://*.9tut.net/*
  7. // @match      http*://*.digitaltut.com/*
  8. // @match      http*://*.certprepare.com/*
  9. // @match      http*://*.networktut.com/*
  10. // @match      http*://*.voicetut.com/*
  11. // @match      http*://*.securitytut.com/*
  12. // @match      http*://*.wirelesstut.com/*
  13. // @match      http*://*.dstut.com/*
  14. // @match      http*://*.rstut.com/*
  15. // @copyright  Public Domain
  16. // ==/UserScript==
  17.  
  18. var correctAns = [];
  19. var availableAns = [];
  20. var qaAnsCount = 0;
  21. var qaQuestCount = 0;
  22.  
  23. processDoc();
  24.  
  25. function checkClass(node) {
  26.     if(node.className != "content") {
  27.         if(node.innerText) {
  28.             if(node.innerText.trim() != "") {
  29.                 if(node.className == "ccnaquestionsnumber")
  30.                     return "Question";
  31.                 if(node.className == "ccnacorrectanswers")
  32.                     return "Answer";
  33.                 if(node.className == "ccnaexplanation")
  34.                     return "Explanation";
  35.                 if(/^Answer\:\s*/.test(node.innerText.trim()))
  36.                     return "Answer";
  37.                 if(/^Question\s+\d+/.test(node.innerText.trim()))
  38.                     return "Question";
  39.             }
  40.         }
  41.     }
  42.     return false;
  43. }
  44.  
  45.  
  46. function processDoc() {
  47.     var post = document.getElementsByClassName("post")[0];
  48.     var content = post.getElementsByClassName("content")[0];
  49.    
  50.     findSec("Question", content);
  51. }
  52.  
  53. function findSec(secName, node) {
  54.     var result = checkClass(node);
  55.     if(result == secName) {
  56.         formatQASection(secName, node);
  57.         return result;
  58.     }
  59.    
  60.     if(node.hasChildNodes()) {
  61.         var i;
  62.         for(i=0; i < node.childNodes.length; i++) {
  63.             result = findSec(secName, node.childNodes[i]);
  64.         }
  65.     }
  66.     return result;
  67. }
  68.  
  69. function findClass(node) {
  70.     var result = false;
  71.    
  72.     result = checkClass(node)
  73.     if(result)
  74.         return result;
  75.    
  76.     if(node.hasChildNodes()) {
  77.         var i;
  78.         for(i=0; i < node.childNodes.length; i++) {
  79.             result = findClass(node.childNodes[i]);
  80.             if(result)
  81.                 break;
  82.         }
  83.     }
  84.    
  85.     return result;
  86. }
  87.  
  88. function formatQASection(secName, secNode) {
  89.     var newNodeFix = secNode;
  90.     while(newNodeFix.parentNode) {
  91.         if(newNodeFix.parentNode.className != "content" && newNodeFix.parentNode.tagName != "DIV")
  92.             newNodeFix = newNodeFix.parentNode;
  93.         else
  94.             break;
  95.     }
  96.  
  97.     var newP = document.createElement("P");
  98.  
  99.     var newNode = null;
  100.     var newNode2 = null;
  101.  
  102.     if(secName == "Question") {
  103.         newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix.nextSibling);
  104.         var answers = [];
  105.         var ansCount = -1;
  106.         var str;
  107.         var newAnsNode = null;
  108.        
  109.         while(newNode.nextSibling) {
  110.             var result = findClass(newNode.nextSibling);
  111.             if(result) {
  112.                 if(result == "Answer") {
  113.                     formatQASection(result, newNode.nextSibling);
  114.                 }
  115.                 break;
  116.             } else {
  117.                 newNode.appendChild(newNode.nextSibling);
  118.             }
  119.         }
  120.        
  121.         if(newNode.hasChildNodes()) {
  122.             var i;
  123.             for(i=0; i < newNode.childNodes.length; i++) {
  124.                 newNode2 = newNode.childNodes[i];
  125.                 if(newNode2.nodeType == 1) {
  126.                     var appendToParent = "";
  127.                     str = newNode2.innerText;
  128.                    
  129.                     if((newNode2.tagName == "P") && (/^\s*([A-K](?!\w)).*$/gm.test(str))) {
  130.                         str = newNode2.outerHTML;
  131.                         var lines = str.split(/\n|<\/?p>|(<[^>]*>)/);
  132.                         var matchCount = 0;
  133.                         for(l=0;l< lines.length;l++) {
  134.                             if(lines[l]) {
  135.                                 lines[l] = lines[l].trim();
  136.                                 if(lines[l] != "") {
  137.                                     if(/^\s*([A-K](?!\w)).*$/.test(lines[l])) {
  138.                                         matchCount++;
  139.                                         if(ansCount < 0) {
  140.                                             if((matchCount == 1) && (lines[l].charAt(0) == "A")) {
  141.                                                 ansCount++;
  142.                                                 answers[ansCount] = lines[l];
  143.                                                 if(!newAnsNode) {
  144.                                                     var newAnsP = document.createElement("P");
  145.                                                     newAnsNode = newNode.insertBefore(newAnsP, newNode2);
  146.                                                 }
  147.                                             } else {
  148.                                                 appendToParent += lines[l];
  149.                                             }
  150.                                         } else {
  151.                                             if(matchCount == 1) {
  152.                                                 if((answers[ansCount].charCodeAt(0)+1) == lines[l].charCodeAt(0)) {
  153.                                                     ansCount++;
  154.                                                     answers[ansCount] = lines[l];
  155.                                                 } else {
  156.                                                     if((lines[l].charAt(0) == "A") && (answers[ansCount].charAt(0) == "A")) {
  157.                                                         appendToParent += answers[ansCount];
  158.                                                         answers[ansCount] = lines[l];
  159.                                                     } else {
  160.                                                         answers[ansCount] += "<br>\n" + lines[l];
  161.                                                     }
  162.                                                 }
  163.                                             } else {
  164.                                                 if(((answers[0].charCodeAt(0)+matchCount-1) == lines[l].charCodeAt(0)) && ((answers[ansCount].charCodeAt(0)+1) == lines[l].charCodeAt(0))) {
  165.                                                     ansCount++;
  166.                                                     answers[ansCount] = lines[l];
  167.                                                 } else {
  168.                                                     answers[ansCount] += "\n" + lines[l];
  169.                                                 }
  170.                                             }
  171.                                         }
  172.                                     } else {
  173.                                         if(ansCount < 0) {
  174.                                             appendToParent += lines[l];
  175.                                             continue;
  176.                                         } else {
  177.                                             answers[ansCount] += "\n" + lines[l];
  178.                                         }
  179.                                     }
  180.                                 }
  181.                             }
  182.                         }
  183.                     } else {
  184.                         if(ansCount < 0) {
  185.                             appendToParent += newNode2.outerHTML;
  186.                         } else {
  187.                             if(newNode2.innerHTML.trim().replace(/(&nbsp;)+$/, "") != "") {
  188.                                 str = newNode2.outerHTML;
  189.                                 answers[ansCount] += "\n" + str;
  190.                             }
  191.                         }
  192.                     }
  193.                     if(appendToParent != "") {
  194.                         var newQuestP = document.createElement("P");
  195.                         newQuestP.innerHTML = appendToParent;
  196.                         if(newAnsNode) {
  197.                             newNode.insertBefore(newQuestP, newAnsNode);
  198.                             i++;
  199.                         } else
  200.                             newNode.insertBefore(newQuestP, newNode2);
  201.                     }
  202.                     newNode.removeChild(newNode2);
  203.                 }
  204.             }
  205.         }
  206.  
  207.         if(newAnsNode) {
  208.             if(answers.length > 1) {
  209.                 availableAns[qaQuestCount] = [];
  210.                 str = "<table>\n";
  211.                 if(correctAns[qaQuestCount]) {
  212.                     if(correctAns[qaQuestCount].length == 1) {
  213.                         for(a=0;a<answers.length;a++) {
  214.                             availableAns[qaQuestCount][a] = answers[a].charAt(0);
  215.                             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"
  216.                         }
  217.                     } else {
  218.                         for(a=0;a<answers.length;a++) {
  219.                             availableAns[qaQuestCount][a] = answers[a].charAt(0);
  220.                             str += "<tr id=ansSelect" + (qaQuestCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"checkbox\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
  221.                         }
  222.                     }
  223.                 } else {
  224.                     for(a=0;a<answers.length;a++) {
  225.                             availableAns[qaQuestCount][a] = answers[a].charAt(0);
  226.                             str += "<tr id=ansSelect" + (qaQuestCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"checkbox\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
  227.                         }
  228.                 }
  229.                 str += "</table>\n"
  230.                 newAnsNode.innerHTML  = str;
  231.                 newAnsNode.normalize();
  232.                 qaQuestCount++;
  233.             } else {
  234.                 var newQuestP = document.createElement("P");
  235.                 newQuestP.innerHTML = answers[0];
  236.                 newNode.insertBefore(newQuestP, newAnsNode);
  237.                 newNode.removeChild(newAnsNode);
  238.             }
  239.         } else {
  240.             if(/^Question\s+\d+/.test(secNode.innerText.trim())) {
  241.                 availableAns[qaQuestCount] = [];
  242.                 if(qaAnsCount == qaQuestCount){
  243.                     correctAns.splice(qaQuestCount, 0, [])
  244.                     qaAnsCount++;
  245.                 }
  246.                 qaQuestCount++;
  247.                
  248.             }
  249.         }
  250.     } else {
  251.         newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix);
  252.  
  253.         var newDiv = document.createElement ("DIV");
  254.         newDiv.style.display = "none";
  255.  
  256.         var newButton = document.createElement("INPUT");
  257.         newButton.setAttribute("type", "button");
  258.         newButton.value = "Show "+secName;
  259.         if(secName == "Answer") {
  260.             var ansCorrect = newNodeFix.innerText.match(/[A-K](?!\w)/gm);
  261.             if(ansCorrect) {
  262.                 correctAns[qaAnsCount] = ansCorrect;
  263.                 newButton.onclick = (function (qaAnsCount, ansCorrect) {
  264.                     return function() {
  265.                         var choices = availableAns[qaAnsCount];
  266.                         if(this.nextSibling.nextSibling.style.display != "none") {
  267.                             this.value = "Show "+secName;
  268.                             this.nextSibling.nextSibling.style.display = "none";
  269.                             if(ansCorrect) {
  270.                                 for(x=0;x<choices.length;x++) {
  271.                                     var trElement = document.getElementById("ansSelect" + (qaAnsCount+1) + choices[x]);
  272.                                     var inputElement = trElement.getElementsByTagName("INPUT")[0];
  273.                                     inputElement.disabled = false;
  274.                                     trElement.style.backgroundColor = "";
  275.                                 }
  276.                             }
  277.                             this.nextSibling.innerText = "";
  278.                             this.nextSibling.style.display = "none";
  279.                         } else {
  280.                             this.value = "Hide "+secName;
  281.                             this.nextSibling.nextSibling.style.display = "block";
  282.                             if(ansCorrect) {
  283.                                 var bCorrect = true;
  284.                                 for(x=0;x<choices.length;x++) {
  285.                                     var trElement = document.getElementById("ansSelect" + (qaAnsCount+1) + choices[x]);
  286.                                     var inputElement = trElement.getElementsByTagName("INPUT")[0];
  287.                                     inputElement.disabled = true;
  288.                                     if(ansCorrect.indexOf(choices[x]) < 0) {
  289.                                         if(inputElement.checked) {
  290.                                             bCorrect = false;
  291.                                             trElement.style.backgroundColor = "red";
  292.                                         }
  293.                                     } else {
  294.                                         if(!inputElement.checked)
  295.                                             bCorrect = false;
  296.                                     }
  297.                                     for(y=0;y<ansCorrect.length;y++) {
  298.                                         if(choices[x] == ansCorrect[y])
  299.                                             trElement.style.backgroundColor = "limegreen";
  300.                                     }
  301.                                 }
  302.                                 if(bCorrect) {
  303.                                     this.nextSibling.style.color = "green";
  304.                                     this.nextSibling.innerText = "\tCORRECT";
  305.                                 } else {
  306.                                     this.nextSibling.style.color = "red";
  307.                                     this.nextSibling.innerText = "\tINCORRECT";
  308.                                 }
  309.                                 this.nextSibling.style.display = "inline";
  310.                             }
  311.                         }
  312.                     }
  313.                 })(qaAnsCount,ansCorrect);
  314.                 qaAnsCount++;
  315.             } else {
  316.                 newButton.onclick = function (){
  317.                     if(this.nextSibling.nextSibling.style.display != "none") {
  318.                         this.value = "Show "+secName;
  319.                         this.nextSibling.nextSibling.style.display = "none";
  320.                     } else {
  321.                         this.value = "Hide "+secName;
  322.                         this.nextSibling.nextSibling.style.display = "block";
  323.                     }
  324.                     return false;
  325.                 };
  326.                
  327.                 if(/^Answer\:\s*/.test(secNode.innerText.trim()))
  328.                     qaAnsCount++;
  329.             }
  330.         } else {
  331.             newButton.onclick = function (){
  332.                 if(this.nextSibling.nextSibling.style.display != "none") {
  333.                     this.value = "Show "+secName;
  334.                     this.nextSibling.nextSibling.style.display = "none";
  335.                 } else {
  336.                     this.value = "Hide "+secName;
  337.                     this.nextSibling.nextSibling.style.display = "block";
  338.                 }
  339.                 return false;
  340.             };
  341.         }
  342.  
  343.         newNode.appendChild(newButton);
  344.  
  345.         var newSpan = document.createElement("SPAN");
  346.         newSpan.id = "ansSpanQ" + qaAnsCount;
  347.         newSpan.style.display = "none";
  348.         newSpan.style.fontWeight = "bold";
  349.  
  350.         newNode.appendChild(newSpan);
  351.  
  352.         newNode2 = newNode.appendChild(newDiv);
  353.         newNode2.appendChild(newNodeFix);
  354.  
  355.         while(newNode.nextSibling) {
  356.             var result = findClass(newNode.nextSibling);
  357.             if(result) {
  358.                 if(result != "Question") {
  359.                     formatQASection(result, newNode.nextSibling);
  360.                 }
  361.                 break;
  362.             } else {
  363.                 newNode2.appendChild(newNode.nextSibling);
  364.             }
  365.         }
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment