Skip to content

Instantly share code, notes, and snippets.

@rayansostenes
Created June 30, 2017 02:38
Show Gist options
  • Save rayansostenes/687b4790e0d0ca54d4ebfedf14ad4c01 to your computer and use it in GitHub Desktop.
Save rayansostenes/687b4790e0d0ca54d4ebfedf14ad4c01 to your computer and use it in GitHub Desktop.
(function (doc) {
function getForm() {
return doc.querySelector('#formQuestao');
}
function getQuestionId() {
const form = getForm();
return parseInt(form.querySelector('input[name="questao_id"]').value, 10);
}
function getRespostas() {
const form = getForm();
return Array.from(form.querySelectorAll('.radio')).map(y => y.innerText.trim().substr(3));
}
function getText() {
const form = getForm();
const textEl = form.querySelector('fieldset .texto');
if (!textEl) return null;
return textEl.innerText.trim();
}
function getEnunciado() {
const form = getForm();
const textEl = form.querySelector('fieldset .enunciado');
if (!textEl) return null;
return textEl.innerText.trim();
}
function getRespostaCerta() {
const form = getForm();
const respostas = ['a', 'b', 'c', 'd', 'e'];
const textEl = form.querySelector('#mensagem-resposta');
if (!textEl) return null;
const index = textEl.innerText.trim().substr(-2, 1).toLowerCase();
return respostas.indexOf(index);
}
function saveLocalStorage(object) {
const key = String(object.id);
const item = JSON.stringify(object);
window.localStorage.setItem(key, item);
}
function getImage() {
const form = getForm();
const textEl = form.querySelector('fieldset img');
if (!textEl) return null;
return textEl.src;
}
function getAllText() {
const form = getForm();
const textEl = form.querySelector('fieldset');
if (!textEl) return null;
return textEl.innerText.trim();
}
function getReturn() {
return {
id: getQuestionId(),
respostas: getRespostas(),
texto: getText(),
enunciado: getEnunciado(),
respostaCerta: getRespostaCerta(),
imagem: getImage(),
allText: getAllText()
};
}
function hasResposta() {
const form = getForm();
return !!form.querySelector('#mensagem-resposta');
}
function init() {
if(hasResposta()){
const resposta = getReturn();
saveLocalStorage(resposta);
console.log(resposta);
doc.querySelector('#btProximo').click();
}else{
console.log('Ainda não tem resposta');
doc.querySelector('.radio input').click();
doc.querySelector('#btResponder').click();
}
}
init();
}(document.querySelector('iframe').contentDocument));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment