|
javascript: (() => { |
|
if (window.BETTER_AMIZONE_LOADED) { |
|
document.removeEventListener('keyup', window.BETTER_AMIZONE_LOADED.eventListener); |
|
delete window.BETTER_AMIZONE_LOADED; |
|
alert("amizone is back to normal (mostly)"); |
|
return; |
|
} |
|
|
|
const querySelectorCurrentQuestion = "[ng-bind-html='questionText(currentQuestion)'] p"; |
|
const querySelectorChoices = "input[type=radio] + label"; |
|
const selectText = (selector) => { |
|
const selection = window.getSelection(); |
|
document.querySelectorAll(selector).forEach((el) => { |
|
const range = document.createRange(); |
|
range.selectNodeContents(el); |
|
selection.addRange(range); |
|
}) |
|
}; |
|
const copySelection = () => { |
|
const selection = window.getSelection(); |
|
const text = selection.toString(); |
|
if (text) { |
|
navigator.clipboard.writeText(text); |
|
} |
|
}; |
|
|
|
/** |
|
* Keyboard navigation for mcq.amizone.net |
|
* - Navigate through questions with right/left ; h/l ; a/d |
|
* - Select choices with 1-5 |
|
* - Alt-q to copy question |
|
* - Alt-a to copy question + options |
|
*/ |
|
const keyupListener = (e) => { |
|
const keysToNext = ['ArrowRight', 'l', 'd']; |
|
const keysToPrev = ['ArrowLeft', 'h', 'a']; |
|
|
|
const btnNext = document.querySelector('button[ng-click="loadNextQue(currentQuestion);"]'); |
|
const btnPrev = document.querySelector('button[ng-click="loadPreQue(currentQuestion);"]'); |
|
|
|
const choiceKeys = [1, 2, 3, 4, 5]; |
|
|
|
if (e.altKey) { |
|
if (e.key === 'q') { |
|
selectText(querySelectorCurrentQuestion); |
|
copySelection(); |
|
return; |
|
} |
|
if (e.key === 'a') { |
|
selectText(querySelectorCurrentQuestion); |
|
selectText(querySelectorChoices); |
|
copySelection(); |
|
return; |
|
} |
|
} |
|
|
|
if (keysToNext.includes(e.key)) { |
|
btnNext.click(); |
|
return; |
|
} |
|
|
|
if (keysToPrev.includes(e.key)) { |
|
btnPrev.click(); |
|
return; |
|
} |
|
|
|
const index = Number(e.key); |
|
if (choiceKeys.includes(index)) { |
|
const checkboxes = document.querySelectorAll('input[type=radio]'); |
|
if (index <= checkboxes.length) { |
|
checkboxes[index - 1].click(); |
|
/* clear selection ;) */ |
|
document.getSelection().removeAllRanges(); |
|
} else { |
|
const currentQuestion = document.querySelector('.ng-binding .pull-left')?.textContent?.match(/\d+/)?.[0] || "?"; |
|
console.debug(`key ${index} doesn't correspond to a checkbox for the current question (${currentQuestion})`); |
|
} |
|
} |
|
}; |
|
|
|
document.addEventListener('keyup', keyupListener); |
|
|
|
/* Enable copy, paste and cut */ |
|
const body = document.querySelector("body"); |
|
body.oncopy = null; |
|
body.onpaste = null; |
|
body.oncut = null; |
|
|
|
window.BETTER_AMIZONE_LOADED = { eventListener: keyupListener }; |
|
alert("amizone is better now :)"); |
|
})(); |