Skip to content

Instantly share code, notes, and snippets.

@milichev
Last active May 10, 2018 13:29
Show Gist options
  • Save milichev/2ec0c67e4de3e2455146cfe0555f8e40 to your computer and use it in GitHub Desktop.
Save milichev/2ec0c67e4de3e2455146cfe0555f8e40 to your computer and use it in GitHub Desktop.
Attempts all form control combinations untill all questions are answered.
($=>{
const getSections = ()=>Array.from($('.questionnaire-text[data-lang="en"] section'));
flip();
function flip() {
let pageTryCount = 0;
const insanePageCount = 40;
const onAjax = ()=>setTimeout(next, 1000);
$(document).on('ajaxSuccess', onAjax);
next();
function next() {
if (window.isEnabled === false && getSections().length > 0) {
tryExam();
return;
}
let confirmCheck = $('#checkbox-confirm:visible');
if (confirmCheck.length > 0) {
confirmCheck.prop('checked', true);
submit();
return;
}
let nextLink = $('.content-wizard .next-item a[href*="assignment/"]:visible');
if (nextLink.length > 0) {
doProgressHolder();
nextLink.click();
return;
}
//submit();
tryExam();
}
function tryExam() {
$(document).off('ajaxSuccess', onAjax);
if (pageTryCount++ >= insanePageCount) {
console.warn('Something has changed, could not flip pages nor answer the test questions :(');
} else if (getSections().length > 0) {
exam();
} else {
console.log('No testing found. Already done?');
}
}
}
function submit() {
//completePage(-382, 23);
doProgressHolder();
let doneButton = $('.done-button, a[href^="#!corporate-assignment/"][data-page]:visible');
if (doneButton.length > 0) {
doneButton[0].scrollIntoView();
doneButton.click();
return true;
}
}
function doProgressHolder() {
let fingerprint;
progressHolder.forEach((value,k)=>{
fingerprint = k;
value.forEach((v,i)=>value[i] = 1);
}
);
if (fingerprint) {
const e = new Event('loadpage');
e.detail = {
pageNumber: 1,
fingerprint
}
window.dispatchEvent(e);
}
}
function exam() {
let ques = getSections().map(function(el) {
let section = $(el);
let inputs = $('.answers input', section).map(function() {
return $(this);
}).toArray();
if (inputs.length === 0) {
return null;
}
let no = parseInt($.trim($('.question-number', section).text()));
const type = inputs.length && inputs[0].prop('type');
return {
section,
no,
inputs,
type
}
}).filter(s=>{
return !!s;
});
//debugger
const findAnswer = q=>{
return Array.from($('.find-answer li', q.section)).map((li)=>{
const text = $(li).text();
let match = text.match(/THE CORRECT ANSWERS? (?:ARE|IS) ([A-Z, ]+)/i);
if (match)
return match[1].replace(/,? AND /i, ', ').split(', ')
return null;
}
).find(a=>!!a);
}
const doHint = q=>{
const hint = findAnswer(q);
if (hint) {
q.inputs.forEach(a=>a[0].checked = false)
const re = new RegExp(`^(?:${hint.join('|')})\\.\\s`,'i');
const hinted = _.filter(q.inputs, i=>i.next().text().match(re));
if (hinted.length > 0) {
hinted.forEach(a=>a[0].checked = true)
return true;
}
}
}
let guessers = {
radio(q) {
if (doHint(q)) {
return;
}
let idx = q.inputs.findIndex(i=>i.is(':checked')) + 1;
if (idx >= q.inputs.length) {
console.warn('Question %d failed every radio answers', q.no);
return;
}
q.inputs[idx].prop('checked', true);
},
checkbox(q) {
if (doHint(q)) {
return;
}
var val = parseInt(q.inputs.reduce((p,i)=>p + (+i.is(':checked')), ''), 2) + 1;
var arr = val.toString(2).split('');
if (arr.length > q.inputs.length) {
console.warn('Question %d failed every checkbox answers', q.no);
return;
}
while (arr.length < q.inputs.length)
arr.unshift('0');
q.inputs.forEach((v,i)=>v.prop('checked', +(arr[i])));
}
};
const onAjax = ()=>setTimeout(guess, 10);
$(document).on('ajaxSuccess', onAjax);
// guess(true);
submit();
function guess(all) {
var pending = _.filter(ques, q=>all || q.section.is('.error'));
if (pending.length > 0) {
console.log('Attempt to guess %s outstanding questions...', pending.length);
pending.forEach(q=>{
if (!guessers[q.type]) {
console.warn(`Unknown quess ${q.no} type ${q.type}`);
}
guessers[q.type](q)
}
);
submit();
} else {
$(document).off('ajaxSuccess', onAjax);
console.log('Done!');
}
}
}
}
)($);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment