Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active April 2, 2019 04:00
Show Gist options
  • Save cuylerstuwe/b565eafe649b75d82ecdfa2dc185e8b8 to your computer and use it in GitHub Desktop.
Save cuylerstuwe/b565eafe649b75d82ecdfa2dc185e8b8 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Quin Typespeak
// @namespace salembeats
// @version 1.4
// @description 1.4 -- Attempts at fixing.
// @author Cuyler Stuwe
// @include https://www.speedtypingonline.com/typing-test*
// @grant none
// ==/UserScript==
async function waitForSelector(selector, intervalTimeMs = 150) {
return await new Promise(resolve => {
const interval = setInterval(() => {
const el = document.querySelector(selector);
if(!!el) {
clearInterval(interval);
resolve(el);
}
}, intervalTimeMs);
});
}
function speakWord(word) {
window.speechSynthesis.cancel();
const msg = new SpeechSynthesisUtterance(word);
// msg.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == "Google español"; })[0];
// msg.rate = 1.5 - (word.length * 0.1);
window.speechSynthesis.speak(msg);
}
async function sync() {
const elements = [...document.querySelectorAll(`#blockDivContainer span`)]
const currentElementIndex = elements.findIndex(element => element.classList.contains("nxtLetter"));
if(
currentElementIndex === 0
|| (currentElementIndex > 0 && elements[currentElementIndex-1].innerText.match(/\s/))
) {
let currentWord = "";
let currentLetter = elements[currentElementIndex].innerText;
let letterIndex = currentElementIndex;
while(!currentLetter.match(/\s/)) {
currentWord += currentLetter;
letterIndex++;
currentLetter = elements[letterIndex].innerText;
}
speakWord(currentWord);
}
}
async function main() {
await waitForSelector(".nxtLetter");
sync();
document.body.addEventListener("keyup", e => {
if(e.target.id === "customTextInput") { return; }
if(e.key.match(/[A-Za-z0-9"']/)) {
sync();
}
}, true);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment