Duolingo keyboard shortcuts

Press Shift+Space to play sentence audio (plus adds number keys support for more word bank exercises)

当前为 2022-09-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Duolingo keyboard shortcuts
  3. // @description Press Shift+Space to play sentence audio (plus adds number keys support for more word bank exercises)
  4. // @version 1.1.4
  5. // @match https://www.duolingo.com/*
  6. // @grant none
  7. // @author szupie szupie@gmail.com
  8. // @namespace szupie
  9. // @license Unlicense
  10. // ==/UserScript==
  11. (function () {
  12. 'use strict';
  13. const speakerButtonSelector = 'button._1KXUd, button._3nOBS, label.sgs9X button, [data-test="speaker-button"]';
  14. function handleKeyboard(e) {
  15. if (e.shiftKey === true && e.key === ' ') {
  16. const speakButton = document.querySelector(speakerButtonSelector);
  17. if (speakButton) {
  18. speakButton.click();
  19. e.preventDefault();
  20. }
  21. }
  22. // use number keys for sentence completion with word bank
  23. if (e.key >= "1" && e.key <= "9") {
  24. if (document.querySelector('[data-test="word-bank"]')) {
  25. const wordBankButtonSelector = `:nth-child(${e.key}) > [data-test="challenge-tap-token"]`;
  26. const wordBankButton = document.querySelector(`[data-test="word-bank"] ${wordBankButtonSelector}:not([aria-disabled])`);
  27. if (wordBankButton) {
  28. wordBankButton.click();
  29. } else {
  30. // remove word from sentence
  31. const disabledClass = '_2Hlc9';
  32. document.querySelectorAll(`._1gad7 :not(.${disabledClass})${wordBankButtonSelector}`).forEach(node => node.click());
  33. }
  34. }
  35. }
  36. }
  37. document.addEventListener('keypress', handleKeyboard, false);
  38. document.addEventListener('keyup', handleKeyboard, false);
  39. })();