Guest User

Userscript for Trivia Trainer keyboard shortcuts

a guest
Mar 5th, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name     Trivia trainer keyboard
  3. // @version  1
  4. // @grant    none
  5. // @description keyboard shortcuts on Trivia Trainer
  6. // @include *www.trivia-trainer.com*
  7. // ==/UserScript==
  8.  
  9. /**************************************
  10. Usage:
  11.  
  12. Pressing the up arrow or t shows and hides the answer
  13. Pressing the left arrow clicks the "correct" button
  14. Pressing the down arrow clicks the "incorrect" button
  15. Pressing the right arrow clicks the "don't know" button
  16.  
  17. Pressing the enter key has the following behaviour:
  18.   -if the answer is hidden, reveal it
  19.   -if the answer is shown, then click the "don't know" button
  20. This is useful if you want to quickly go through questions
  21. while you're not logged in by pressing only one key.
  22. Obviously don't use this if you're logged in and don't want
  23. to mess up your stats.
  24.  
  25.  
  26. Other notes:
  27. You can easily customize this script by messing around with
  28. the "keyDown" function.
  29. Relevant keycodes can be found at keycode.info.
  30.  
  31. Don't be mad at me for using keycodes; I learned they were
  32. deprecated AFTER I wrote this script.
  33. **************************************/
  34.  
  35. var node = document.getElementById("Answer");
  36. var button_correct   = document.forms[0].elements[3];
  37. var button_incorrect = document.forms[0].elements[4];
  38. var button_dontknow  = document.forms[0].elements[5];
  39.  
  40. // reveals and hides answer
  41. // triggered by t or spacebar
  42. function toggleAnswer()
  43. {
  44.   if (node.getAttribute("aria-expanded") == "true") {
  45.       node.setAttribute("aria-expanded", "false");
  46.         node.setAttribute("class", "collapse");
  47.     } else {
  48.         node.setAttribute("aria-expanded", "true");
  49.         node.setAttribute("class", "collapse in");
  50.     }
  51. }
  52.  
  53. // expands answer if hidden, submits with "don't know" if answer is revealed
  54. // triggered by enter key
  55. function forward()
  56. {
  57.   if (node.getAttribute("aria-expanded") == "true") {
  58.         button_dontknow.setAttribute("class", "quiz-answer"); // dumb hack to highlight button
  59.       button_dontknow.click();
  60.     } else {
  61.         node.setAttribute("aria-expanded", "true");
  62.         node.setAttribute("class", "collapse in");
  63.     }
  64. }
  65.  
  66. // submit with "correct", "incorrect", or "don't know"
  67. // triggered by arrow keys
  68. function submitAnswer(button)
  69. {
  70.   button.setAttribute("class", "quiz-answer");
  71.   button.click();
  72. }
  73.  
  74. function keyDown(evt)
  75. {
  76.   switch (evt.keyCode) {
  77.     case 84: // t
  78.       toggleAnswer();
  79.       break;
  80.     case 38: // up arrow
  81.       evt.preventDefault();
  82.       toggleAnswer();
  83.       break;
  84.     case 13: // enter
  85.       forward();
  86.       break;
  87.     case 37: // left arrow
  88.       submitAnswer(button_correct);
  89.       break;
  90.     case 40: // down arrow
  91.       evt.preventDefault();
  92.       submitAnswer(button_incorrect);
  93.       break;
  94.     case 39: // right arrow
  95.       submitAnswer(button_dontknow);
  96.       break;
  97.   }
  98.  
  99. }
  100.  
  101. document.addEventListener('keydown', keyDown, true);
Advertisement
Add Comment
Please, Sign In to add comment