Duolingo audio keyboard shortcut

Press Shift+Space to play sentence audio

当前为 2021-07-06 提交的版本,查看 最新版本

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