Skip to content

Instantly share code, notes, and snippets.

@SwapnilSoni1999
Last active August 29, 2020 08:45
Show Gist options
  • Save SwapnilSoni1999/2a095f8ddcbd3a0387fa99d5a69a83fd to your computer and use it in GitHub Desktop.
Save SwapnilSoni1999/2a095f8ddcbd3a0387fa99d5a69a83fd to your computer and use it in GitHub Desktop.
google form auto filling script
// extractor works after submitting form and clicking on [View Score] button
// once all correct answers are displayed use this script
function isCorrect(el) {
const val = el.querySelector('[aria-label="Incorrect"]')
return val ? false:true
}
function answerFromIncorrect(el) {
return el.querySelector("div.freebirdFormviewerViewItemsItemGradingCorrectAnswerBoxContent > div > label > div > div.docssharedWizToggleLabeledContent > div > span").innerText.split('.')[0]
}
function answerFromCorrect(el) {
return el.getElementsByClassName('appsMaterialWizToggleRadiogroupEl exportToggleEl isChecked isDisabled')[0].getAttribute('data-value').split('.')[0]
}
var arr = Array.from(document.querySelectorAll("body > div > div:nth-child(2) > div.freebirdFormviewerViewFormCard.exportFormCard > div > div.freebirdFormviewerViewItemList > div"))
var questions = arr.slice(7)
var answers = []
for (let i=0; i<questions.length; i++) {
if(isCorrect(questions[i])) {
const answer = answerFromCorrect(questions[i])
answers.push({ i: answer })
} else {
const answer = answerFromIncorrect(questions[i])
answers.push({ i: answer })
}
}
console.log(answers)
function getIntFromMCQ(optionStr) {
const OptEnum = { 'A': 0, 'B': 1, 'C': 2, 'D': 3 }
return OptEnum[optionStr]
}
function autofillAnswers(answerSeriesStr) {
let answers = answerSeriesStr.match(/\w/gm)
for (let i=0; i<answers.length; i++) {
let currQues = i
let answer = answers[i]
document.querySelectorAll('.freebirdFormviewerViewItemsItemItem')[currQues].children[1].children[0].children[0].children[0].children[getIntFromMCQ(answer)].querySelector('label').click()
}
}
// usage autofillAnswers("DBCCBDBCADCBADBADABC")
// change slice according to your need
myarr = Array.from(document.querySelectorAll("#mG61Hd > div.freebirdFormviewerViewFormCard.exportFormCard > div > div.freebirdFormviewerViewItemList > div")).slice(7)
function getIntFromMCQ(optionStr) {
const OptEnum = { 'A': 0, 'B': 1, 'C': 2, 'D': 3 }
return OptEnum[optionStr]
}
function autoFillAnswers(answerStr) {
const ans = answerStr.split('')
for (let i=0; i<ans.length; i++) {
myarr[i].children[0].children[0].children[1].children[1].querySelectorAll('label')[getIntFromMCQ(ans[i])].click()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment