Guest User

9tut Q&A Mod 0.4a

a guest
Sep 6th, 2014
575
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.4a
  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.  
  20. formatQASection("ccnacorrectanswers", "Answer");
  21. formatQASection("ccnaquestionsnumber", "Question");
  22. formatQASection("ccnaexplanation", "Explanation");
  23.  
  24. function findClass(node) {
  25.     if((node.className == "ccnaexplanation") || (node.className == "ccnaquestionsnumber") || (node.className == "ccnacorrectanswers")) {
  26.         if(node.innerHTML.trim() != "")
  27.             return true;
  28.     }
  29.    
  30.     var result = false;
  31.    
  32.     if(node.childNodes.length > 0) {
  33.         var i;
  34.         for(i=0; i < node.childNodes.length; i++) {
  35.             result = findClass(node.childNodes[i]);
  36.             if(result)
  37.                 break;
  38.         }
  39.     }
  40.    
  41.     return result;
  42. }
  43.  
  44. function formatQASection(className, secName) {
  45.     var c = document.getElementsByClassName("post");
  46.     var x = c[0].getElementsByClassName(className);
  47.     var qaCount = 0;
  48.    
  49.     for (i = 0; i < x.length; i++) {
  50.         if(x[i].innerHTML.trim() == "") continue;
  51.            
  52.         var newNodeFix = x[i];
  53.         while(newNodeFix.parentNode) {
  54.             if(newNodeFix.parentNode.className != "content" && newNodeFix.parentNode.tagName != "DIV")
  55.                 newNodeFix = newNodeFix.parentNode;
  56.             else
  57.                 break;
  58.         }
  59.            
  60.         var newP = document.createElement("P");
  61.        
  62.         var newNode = null;
  63.         var newNode2 = null;
  64.            
  65.         if(className == "ccnaquestionsnumber") {
  66.             newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix.nextSibling);
  67.             var answers = [];
  68.             var ansCount = -1;
  69.             var str;
  70.             var newAnsNode = null;
  71.            
  72.             if(correctAns[qaCount]) {
  73.                 while(newNode.nextSibling) {
  74.                     if(findClass(newNode.nextSibling)) {
  75.                         break;
  76.                     } else {
  77.                         newNode2 = newNode.appendChild(newNode.nextSibling);
  78.  
  79.                         if(newNode2.nodeType == 1) {
  80.                             str = newNode2.innerText;
  81.  
  82.                             var matches = str.match(/^\s*([A-K]\s*(\.|\-|\–|\—)).*(?!\?){1}/gm);
  83.                             if(matches) {
  84.                                 if(matches.length >= 1) {
  85.                                     var lines = str.split(/\n/);
  86.                                     for(l=0;l< lines.length;l++) {
  87.                                         if(/^(\s*)(([A-K]\s*(\.|\-|\–|\—)){1}.*)/.test(lines[l])) {
  88.                                             ansCount++;
  89.                                             answers[ansCount] = lines[l].trim();
  90.                                             if(!newAnsNode) {
  91.                                                 var newAnsP = document.createElement("P");
  92.                                                 newAnsNode = newNode.appendChild(newAnsP);
  93.                                             }
  94.                                         } else {
  95.                                             if(ansCount < 0) {
  96.                                                 var newQuestP = document.createElement("P");
  97.                                                 newQuestP.innerHTML = lines[l];
  98.                                                 newNode.appendChild(newQuestP);
  99.                                                 continue;
  100.                                             }
  101.                                             str = lines[l].trim();
  102.                                             if(str != "")
  103.                                                 answers[ansCount] += str + "<br>";
  104.                                         }
  105.                                     }
  106.                                     newNode.removeChild(newNode2);
  107.                                 }
  108.                             }
  109.                         }
  110.                     }
  111.                 }
  112.  
  113.                 if(newAnsNode) {
  114.                     str = "<table>";
  115.                     if(correctAns[qaCount].length == 1) {
  116.                         for(a=0;a<answers.length;a++) {
  117.                             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"
  118.                         }
  119.                     } else {
  120.                         for(a=0;a<answers.length;a++) {
  121.                             str += "<tr id=ansSelect" + (qaCount+1) + answers[a].charAt(0) + ">\n<td><input type=\"checkbox\"></td>\n<td>" + answers[a] + "</td>\n</tr>\n"
  122.                         }
  123.                     }
  124.                     str += "</table>\n"
  125.                     newAnsNode.innerHTML  = str;
  126.                 } else
  127.                     qaCount--;
  128.             }
  129.         } else {
  130.             newNode = newNodeFix.parentNode.insertBefore(newP, newNodeFix);
  131.            
  132.             var newDiv = document.createElement ("DIV");
  133.             newDiv.style.display = "none";
  134.            
  135.             var newButton = document.createElement("INPUT");
  136.             newButton.setAttribute("type", "button");
  137.             newButton.value = "Show "+secName;
  138.             if(className == "ccnacorrectanswers") {
  139.                 var ansCorrect = newNodeFix.innerText.match(/[A-K](?!\w)/gm);
  140.                 if(ansCorrect) {
  141.                     correctAns[qaCount] = ansCorrect;
  142.                     newButton.onclick = (function (qaCount, ansCorrect) {
  143.                         return function() {
  144.                             if(this.nextSibling.style.display != "none") {
  145.                                 this.value = "Show "+secName;
  146.                                 this.nextSibling.style.display = "none";
  147.                                 if(ansCorrect) {
  148.                                     for(x=0;x<ansCorrect.length;x++)
  149.                                         document.getElementById("ansSelect" + (qaCount+1) + ansCorrect[x]).style.backgroundColor = "";
  150.                                 }
  151.                             } else {
  152.                                 this.value = "Hide "+secName;
  153.                                 this.nextSibling.style.display = "block";
  154.                                 if(ansCorrect) {
  155.                                     for(x=0;x<ansCorrect.length;x++)
  156.                                         document.getElementById("ansSelect" + (qaCount+1) + ansCorrect[x]).style.backgroundColor = "limegreen";
  157.                                 }
  158.                             }
  159.                         }
  160.                     })(qaCount,ansCorrect);
  161.                 } else {
  162.                     newButton.onclick = function (){
  163.                         if(this.nextSibling.style.display != "none") {
  164.                             this.value = "Show "+secName;
  165.                             this.nextSibling.style.display = "none";
  166.                         } else {
  167.                             this.value = "Hide "+secName;
  168.                             this.nextSibling.style.display = "block";
  169.                         }
  170.                         return false;
  171.                     };
  172.                     qaCount--;
  173.                 }
  174.             } else {
  175.                 newButton.onclick = function (){
  176.                     if(this.nextSibling.style.display != "none") {
  177.                         this.value = "Show "+secName;
  178.                         this.nextSibling.style.display = "none";
  179.                     } else {
  180.                         this.value = "Hide "+secName;
  181.                         this.nextSibling.style.display = "block";
  182.                     }
  183.                     return false;
  184.                 };
  185.             }
  186.            
  187.             newNode.appendChild(newButton);
  188.            
  189.             newNode2 = newNode.appendChild(newDiv);
  190.             newNode2.appendChild(newNodeFix);
  191.            
  192.             while(newNode.nextSibling) {
  193.                 if(findClass(newNode.nextSibling))
  194.                     break;
  195.                 else
  196.                     newNode2.appendChild(newNode.nextSibling);
  197.             }
  198.         }
  199.         qaCount++;
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment