Skip to content

Instantly share code, notes, and snippets.

@trulysinclair
Created June 25, 2023 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trulysinclair/1f578a6d07855a9a5e7a4056901a91f3 to your computer and use it in GitHub Desktop.
Save trulysinclair/1f578a6d07855a9a5e7a4056901a91f3 to your computer and use it in GitHub Desktop.
Simple cheat for typing tests.
// Get the parent element by its class names
const parentElement = document.querySelector('.css-1rw37je.eu4oa1w0');
// Check if the parent element exists
if (parentElement) {
// Get all the child spans of the parent element
const childSpans = parentElement.querySelectorAll('span');
// Create an array to store the words
const wordsArray = [];
// Iterate over each child span and extract the word
childSpans.forEach(span => {
const word = span.textContent.trim();
wordsArray.push(word);
});
// Get the input element by its class names
const inputElement = document.querySelector('.css-11a28ok.e1jgz0i2');
// Check if the input element exists
if (inputElement) {
// Function to simulate typing effect
const typeSentence = () => {
let sentence = '';
let currentIndex = 0;
let currentCharacterIndex = 0;
// Define an interval to simulate typing
const interval = setInterval(() => {
// Get the current word
const currentWord = wordsArray[currentIndex];
// Get the current character
const currentCharacter = currentWord[currentCharacterIndex];
// Add the current character to the sentence
sentence += currentCharacter;
// Set the value of the input element to the sentence
inputElement.value = sentence;
// Create a keydown event with the relative key
const keyEvent = new KeyboardEvent('keydown', {
key: currentCharacter,
keyCode: currentCharacter.charCodeAt(0),
which: currentCharacter.charCodeAt(0)
});
// Dispatch the keydown event
inputElement.dispatchEvent(keyEvent);
// Move to the next character
currentCharacterIndex++;
// If all characters of the current word have been typed
if (currentCharacterIndex === currentWord.length) {
// Reset the character index for the next word
currentCharacterIndex = 0;
// Move to the next word
currentIndex++;
// If there are more words, add a space after the word
if (currentIndex !== wordsArray.length) {
// Add a space to the sentence
sentence += ' ';
// Create a space keydown event
const spaceKeyEvent = new KeyboardEvent('keydown', {
key: ' ',
keyCode: 32,
which: 32
});
// Dispatch the space keydown event
inputElement.dispatchEvent(spaceKeyEvent);
}
}
// If all words have been typed, clear the interval
if (currentIndex === wordsArray.length) {
clearInterval(interval);
}
}, 100); // Adjust the typing speed (in milliseconds) as per your preference
};
// Call the function to start simulating typing
typeSentence();
} else {
console.log('Input element not found.');
}
} else {
console.log('Parent element not found.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment