Skip to content

Instantly share code, notes, and snippets.

@szupie
Last active May 25, 2024 15:03
Show Gist options
  • Save szupie/b049d0282e206ee78ab50fa68f10c172 to your computer and use it in GitHub Desktop.
Save szupie/b049d0282e206ee78ab50fa68f10c172 to your computer and use it in GitHub Desktop.
Duolingo keyboard shortcuts
// ==UserScript==
// @name Duolingo keyboard shortcuts
// @description Press Shift+Space to play sentence audio (plus adds number keys support for more word bank exercises)
// @version 1.1.8
// @match https://www.duolingo.com/*
// @grant none
// @author szupie szupie@gmail.com
// @namespace szupie
// @license Unlicense
// ==/UserScript==
(function () {
'use strict';
const speakerButtonSelector = '[dir][lang] [dir="ltr"] > button, button._2iVKu, button.L2pR0, button._3MUaZ, button._2UR3y, button.hWH3-, button._3TlAm, button._29LnD, button._2sNVM, button._1oX8u, button._1KXUd, button._3nOBS, label.sgs9X button, [data-test="speaker-button"]';
function handleKeyboard(e) {
if (e.shiftKey === true && e.key === ' ') {
const speakButton = document.querySelector(speakerButtonSelector);
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([aria-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);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment