// ==UserScript== // @name Duolingo audio keyboard shortcut // @description Press Shift+Space to play sentence audio // @version 1.1 // @match https://www.duolingo.com/* // @grant none // @author szupie szupie@gmail.com // @namespace szupie // @downloadURL none // ==/UserScript== (function () { 'use strict'; const selector = '[data-test="speaker-button"]'; function handleKeyboard(e) { if (e.shiftKey === true && e.key === ' ') { const speakButton = document.querySelector(selector); if (speakButton) { speakButton.click(); } e.preventDefault(); } // use number keys for sentence completion with word bank if (e.key >= "1" && e.key <= "9") { if (document.querySelector('[data-test="word-bank"]')) { const wordBankButtonSelector = `:nth-child(${e.key}) > [data-test="challenge-tap-token"]`; const wordBankButton = document.querySelector(`[data-test="word-bank"] ${wordBankButtonSelector}:not([disabled])`); if (wordBankButton) { wordBankButton.click(); } else { // remove word from sentence const disabledClass = '_2Hlc9'; document.querySelectorAll(`._1gad7 :not(.${disabledClass})${wordBankButtonSelector}`).forEach(node => node.click()); } } } } document.addEventListener('keypress', handleKeyboard, false); document.addEventListener('keyup', handleKeyboard, false); })();