Advertisement
octoshrimpy

Untitled

Feb 24th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. // total question count
  2. var questionNum;
  3. // array of questions and current index of answer
  4. var questionsArray = {}
  5. // whether to answer or not.
  6. // toggle this boolean true/false to start/stop
  7. var autoAnswer = false;
  8.  
  9. // button
  10. var auto_btn = document.createElement("button");
  11. auto_btn.innerHTML = "waiting for page to load. . .";
  12.  
  13. //answer according to index array
  14. function startAnswering(){
  15.  
  16. //setup vars
  17. questionNum = getCurrentQuestion()["totalCount"];
  18.  
  19. //reset startup var now that we're in the loop
  20. autoAnswer = false;
  21.  
  22. do{
  23.  
  24. //get question info
  25. var getQ = getCurrentQuestion();
  26. var currentQ = getQ["questionStr"];
  27. var currentQcount = parseInt(getQ["count"]);
  28. // logging to keep track of where it is
  29. console.log(`${currentQcount} / ${questionNum}`);
  30.  
  31. var answerCount;
  32. // get which answer based on dictionary
  33. if(questionsArray[currentQ] === undefined) {
  34. answerCount = 0;
  35. } else {
  36. answerCount = questionsArray[currentQ];
  37. }
  38.  
  39. // if it wasn't in the dictionary, add it.
  40. if(!questionsArray[currentQ]){
  41. questionsArray[currentQ] = parseInt(answerCount);
  42. }
  43.  
  44. // answer
  45. dom_answer(answerCount);
  46. // click next
  47. dom_next();
  48. } while (currentQcount < questionNum);
  49. // click done + look for bad answers after the quiz
  50. window.setTimeout(function(){dom_done()}, 1000);
  51. window.setTimeout(function(){checkBadAnswers()}, 2000);
  52.  
  53. }
  54.  
  55. // get bad answers and advance index by one
  56. // if A was wrong, try B next round
  57. function checkBadAnswers(){
  58. // get ul and li children
  59. var listWrap = document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-body > div > div:nth-child(2) > div > div > ul");
  60. var children = listWrap.getElementsByTagName("li");
  61.  
  62. // iterate through li
  63. for (var i = 0; i < children.length; i++) {
  64. var child = children[i];
  65. var question = child.textContent;
  66.  
  67. // iterate through index one
  68. questionsArray[question]++;
  69. }
  70.  
  71. // close the quiz, and retake it if not passed
  72. document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-footer > div.comp > button.btn.btn-cancel.fastclickable").click();
  73. document.body.querySelector(`#kb > div > div.pad.col-xs-12.col-lg-10.col-lg-offset-1.fullArticle > div.row.artComp > div > div:nth-child(2) > button`).click();
  74.  
  75. // get take quiz button and check for "quizz passed"
  76. var quizText = document.body.querySelector("#kb > div > div.pad.col-xs-12.col-lg-10.col-lg-offset-1.fullArticle > div.row.artComp > div > div:nth-child(2) > button").textContent;
  77. if(quizText ==="Take Quiz") {
  78. window.setTimeout(function(){
  79. startAnswering();
  80. }, 500);
  81. } else {
  82. auto_btn.innerHTML = "article completed";
  83. }
  84. }
  85.  
  86.  
  87. // helper funcs
  88. // gets question number, total Qs, and the question string
  89. function getCurrentQuestion(){
  90. var questionStr = document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-body > h2").textContent;
  91. var countStr = document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-header > div > h3 > span").textContent;
  92. var count = countStr.split(" of ")[0];
  93. var totalCount = countStr.split(" of ")[1];
  94.  
  95. return {questionStr, count, totalCount};
  96. }
  97.  
  98. // click the right index-based answer
  99. function dom_answer(count) {
  100. innerCount = count + 1;
  101. var answersWrap = document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-body > div > div > ul");
  102. var child = answersWrap.querySelector(`li:nth-child(${innerCount}) [ng-bind-html="answer | html"]`);
  103. child.click();
  104. }
  105.  
  106. // click next
  107. function dom_next() {
  108. document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-footer > div.qnav > div > button.btn.btn-confirm.fastclickable").click();
  109. }
  110.  
  111. // click done
  112. function dom_done() {
  113. document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-footer > div.comp > button.btn.btn-confirm.fastclickable").click();
  114. }
  115.  
  116. // click start quiz
  117. function dom_start() {
  118. document.body.querySelector("#kb > div > div.pad.col-xs-12.col-lg-10.col-lg-offset-1.fullArticle > div.row.artComp > div > div:nth-child(2) > button").click();
  119. }
  120.  
  121. // create "autoanswer button" in dom
  122. function create_auto_btn() {
  123. var old_btn = document.body.querySelector('.auto-btn');
  124. if(isInPage(old_btn)) {
  125. old_btn.remove()
  126. }
  127.  
  128. // button created above at top
  129.  
  130. auto_btn.style.cssText = `
  131. position: fixed;
  132. top: 7rem;
  133. right: 2rem;
  134. text-transform: uppercase;
  135. font-weight: 100;
  136. font-size: 1.5rem;
  137. padding: 1rem;
  138. border-radius: 5px;
  139. background: #34495e;
  140. color: #27ae60;
  141. `;
  142.  
  143. document.body.appendChild(auto_btn);
  144. auto_btn.classList.add('auto-btn');
  145.  
  146. if( article_completed() ) {
  147. auto_btn.innerHTML = "article completed!";
  148. return;
  149. } else {
  150.  
  151.  
  152. auto_btn.innerHTML = "Start auto-quiz";
  153. auto_btn.addEventListener('click', event => {
  154. dom_start();
  155. window.setTimeout(function(){
  156. startAnswering();
  157. }, 500);
  158.  
  159. })
  160. }
  161. }
  162.  
  163. function article_completed() {
  164. var quizText = document.body.querySelector("#kb > div > div.pad.col-xs-12.col-lg-10.col-lg-offset-1.fullArticle > div.row.artComp > div > div:nth-child(2) > button").textContent;
  165. if(quizText === " Completed Article") {
  166. return true;
  167. }
  168. return false;
  169. }
  170.  
  171. //startup cleaning
  172. function startup_cleaning() {
  173. console.log()
  174. var remind_later = document.body.querySelector("#requiredTraining > div.modal-header > h3 > button");
  175. var next_training = document.body.querySelector("#requiredTraining > div.modal-body > div:nth-child(2) > table > tbody > tr:nth-child(1) > td:nth-child(1) > button");
  176.  
  177. if(isInPage(remind_later)) {
  178. remind_later.click();
  179. }
  180.  
  181. create_auto_btn();
  182. }
  183.  
  184. function isInPage(node) {
  185. return (node === document.body) ? false : document.body.contains(node);
  186. }
  187.  
  188. // end helper funcs
  189.  
  190. // start'er up
  191.  
  192. function ready(fn) {
  193. if (document.readyState != 'loading'){
  194. fn();
  195. } else {
  196. document.addEventListener('DOMContentLoaded', fn);
  197. }
  198. }
  199.  
  200. ready( startup_cleaning );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement