Last active
June 7, 2019 15:50
-
-
Save nmanumr/6a902240c46c2059542e47b3e901eb67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var handlers = { | |
"GetAttemptedQuizDetails": detailPageHandler, | |
"PopulateSingleQuiz": McqPageHandler, | |
} | |
String.prototype.hashCode = function () { | |
var hash = 0, i, chr; | |
if (this.length === 0) return hash; | |
for (i = 0; i < this.length; i++) { | |
chr = this.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; // Convert to 32bit integer | |
} | |
return hash; | |
} | |
function main() { | |
for (var handler in handlers) { | |
var re = new RegExp(handler, "i"); | |
if (re.exec(window.location.pathname)) { | |
handlers[handler](); | |
} | |
} | |
} | |
function detailPageHandler() { | |
var qDict = JSON.parse(localStorage['qDict'] || '{}'); | |
var parser = new QuizDetailParser(); | |
var questions = parser.parse(document); | |
for(var question of questions){ | |
var mcq = MCQ.loadFromObj(question); | |
var savedMcq = mcq.existsIn(qDict); | |
if (savedMcq) { | |
savedMcq.updateAnsFromText(question.correctAns) | |
qDict[savedMcq.getHash()] = savedMcq.getJSON(); | |
console.log(`UPDATED: ${question.text.innerText}`); | |
} | |
} | |
localStorage['qDict'] = JSON.stringify(qDict); | |
} | |
function McqPageHandler() { | |
var qDict = JSON.parse(localStorage['qDict'] || '{}'); | |
var parser = new McqParser(); | |
var parsedMcq = parser.parse(document); | |
var savedMcq = parsedMcq.existsIn(qDict) | |
if (savedMcq) { | |
parsedMcq.mark(savedMcq.ans); | |
console.log("Question found"); | |
console.log("Known correct option marked"); | |
} | |
else { | |
console.log("Question not found"); | |
document.querySelector(`.submit_btn`).setAttribute('onclick', '') | |
document.querySelectorAll("[name='quiz_option']") | |
.forEach((el) => { | |
el.addEventListener("click", (e) => { | |
parsedMcq.setAns(e.target.value); | |
}); | |
}); | |
document.querySelector(`.submit_btn`) | |
.addEventListener("click", () => { | |
qDict[parsedMcq.getHash()] = parsedMcq.getJSON(); | |
localStorage['qDict'] = JSON.stringify(qDict); | |
document.forms[0].submit(); | |
}) | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment