Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // total question count
- var questionNum;
- // array of questions and current index of answer
- var questionsArray = {}
- // whether to answer or not.
- // toggle this boolean true/false to start/stop
- var autoAnswer = false;
- // button
- var auto_btn = document.createElement("button");
- auto_btn.innerHTML = "waiting for page to load. . .";
- //answer according to index array
- function startAnswering(){
- //setup vars
- questionNum = getCurrentQuestion()["totalCount"];
- //reset startup var now that we're in the loop
- autoAnswer = false;
- do{
- //get question info
- var getQ = getCurrentQuestion();
- var currentQ = getQ["questionStr"];
- var currentQcount = parseInt(getQ["count"]);
- // logging to keep track of where it is
- console.log(`${currentQcount} / ${questionNum}`);
- var answerCount;
- // get which answer based on dictionary
- if(questionsArray[currentQ] === undefined) {
- answerCount = 0;
- } else {
- answerCount = questionsArray[currentQ];
- }
- // if it wasn't in the dictionary, add it.
- if(!questionsArray[currentQ]){
- questionsArray[currentQ] = parseInt(answerCount);
- }
- // answer
- dom_answer(answerCount);
- // click next
- dom_next();
- } while (currentQcount < questionNum);
- // click done + look for bad answers after the quiz
- window.setTimeout(function(){dom_done()}, 1000);
- window.setTimeout(function(){checkBadAnswers()}, 2000);
- }
- // get bad answers and advance index by one
- // if A was wrong, try B next round
- function checkBadAnswers(){
- // get ul and li children
- 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");
- var children = listWrap.getElementsByTagName("li");
- // iterate through li
- for (var i = 0; i < children.length; i++) {
- var child = children[i];
- var question = child.textContent;
- // iterate through index one
- questionsArray[question]++;
- }
- // close the quiz, and retake it if not passed
- 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();
- 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();
- // get take quiz button and check for "quizz passed"
- 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;
- if(quizText ==="Take Quiz") {
- window.setTimeout(function(){
- startAnswering();
- }, 500);
- } else {
- auto_btn.innerHTML = "article completed";
- }
- }
- // helper funcs
- // gets question number, total Qs, and the question string
- function getCurrentQuestion(){
- var questionStr = document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-body > h2").textContent;
- 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;
- var count = countStr.split(" of ")[0];
- var totalCount = countStr.split(" of ")[1];
- return {questionStr, count, totalCount};
- }
- // click the right index-based answer
- function dom_answer(count) {
- innerCount = count + 1;
- var answersWrap = document.body.querySelector("body > div.modal.fade.fastclickable.portal-base.kbase-training-modal.in > div > div > div.modal-body > div > div > ul");
- var child = answersWrap.querySelector(`li:nth-child(${innerCount}) [ng-bind-html="answer | html"]`);
- child.click();
- }
- // click next
- function dom_next() {
- 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();
- }
- // click done
- function dom_done() {
- 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();
- }
- // click start quiz
- function dom_start() {
- 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();
- }
- // create "autoanswer button" in dom
- function create_auto_btn() {
- var old_btn = document.body.querySelector('.auto-btn');
- if(isInPage(old_btn)) {
- old_btn.remove()
- }
- // button created above at top
- auto_btn.style.cssText = `
- position: fixed;
- top: 7rem;
- right: 2rem;
- text-transform: uppercase;
- font-weight: 100;
- font-size: 1.5rem;
- padding: 1rem;
- border-radius: 5px;
- background: #34495e;
- color: #27ae60;
- `;
- document.body.appendChild(auto_btn);
- auto_btn.classList.add('auto-btn');
- if( article_completed() ) {
- auto_btn.innerHTML = "article completed!";
- return;
- } else {
- auto_btn.innerHTML = "Start auto-quiz";
- auto_btn.addEventListener('click', event => {
- dom_start();
- window.setTimeout(function(){
- startAnswering();
- }, 500);
- })
- }
- }
- function article_completed() {
- 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;
- if(quizText === " Completed Article") {
- return true;
- }
- return false;
- }
- //startup cleaning
- function startup_cleaning() {
- console.log()
- var remind_later = document.body.querySelector("#requiredTraining > div.modal-header > h3 > button");
- 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");
- if(isInPage(remind_later)) {
- remind_later.click();
- }
- create_auto_btn();
- }
- function isInPage(node) {
- return (node === document.body) ? false : document.body.contains(node);
- }
- // end helper funcs
- // start'er up
- function ready(fn) {
- if (document.readyState != 'loading'){
- fn();
- } else {
- document.addEventListener('DOMContentLoaded', fn);
- }
- }
- ready( startup_cleaning );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement