Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MatthewScholefield/d6170bc988df1297d98f77533293fc82 to your computer and use it in GitHub Desktop.
Save MatthewScholefield/d6170bc988df1297d98f77533293fc82 to your computer and use it in GitHub Desktop.
User script to control Duolingo with Keyboard
// ==UserScript==
// @name Duolingo Keyboard Control
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add full keyboard control to Duolingo (to tiles). Shortcuts: 0-9, shift + 0-9, and "\"
// @author Matthew Scholefield
// @match https://www.duolingo.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Dynamic names of parent elements of the button containers.
const buttonParent1 = '._1PGxI', buttonParent2 = '._30i_q', buttonParent3 = '._30i_q', sentParent1 = '._6HogY', sentParent2 = '_2T9b4';
const charToKey = {
'!': 1,
'@': 2,
'#': 3,
'$': 4,
'%': 5,
'^': 6,
'&': 7,
'*': 8,
'(': 9,
')': 0
};
const getNum = d => d == 0 ? 9 : d - 1;
document.addEventListener('keypress', ({ ctrlKey, shiftKey, key }) => {
var parent = document.querySelector(buttonParent1) || document.querySelector(buttonParent2);
var sentParent = document.querySelector(sentParent1) || document.querySelector(sentParent2);
var d = parseInt(key);
if (!isNaN(d)) {
parent.childNodes[getNum(d)].click();
}
if (key === '\\') {
sentParent.childNodes[sentParent.childNodes.length - 1].click();
}
d = charToKey[key];
if (d !== undefined) {
hoverParent.childNodes[getNum(d)].click();
}
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment