Created
May 12, 2019 07:45
-
-
Save MatthewScholefield/d6170bc988df1297d98f77533293fc82 to your computer and use it in GitHub Desktop.
User script to control Duolingo with Keyboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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