Skip to content

Instantly share code, notes, and snippets.

@ditsuke
Last active May 16, 2023 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ditsuke/24318a983a990460fbaff9dfaa0e5d77 to your computer and use it in GitHub Desktop.
Save ditsuke/24318a983a990460fbaff9dfaa0e5d77 to your computer and use it in GitHub Desktop.

Better MCQ.Amizone ☀️

This bookmarklet allows you to navigate Amizone Multiple Choice Questions (MCQs) using only your keyboard, without having to use your mouse. It also enables copy/pasting.

Installation

To install the bookmarklet:

  1. Open your web browser (Firefox or Chrome).
  2. Create a new bookmark by clicking the star icon in the address bar.
  3. Name the bookmark something memorable, like "Amizone MCQ Keyboard Navigation", and paste the bookmarklet code into the URL field.
  4. Save the bookmark to your bookmarks bar.

Usage

Once you have installed the bookmarklet, navigate to the Amizone MCQ page and click on the bookmarklet to activate it. You can then navigate the quiz entirely with your keyboard:

  1. Select choices with 1-5.
  2. Move between questions with Arrow keys, H/L or A/D.
  3. Select and copy question with Alt-Q, question+choices with Alt-A.
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 :)");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment