Skip to content

Instantly share code, notes, and snippets.

@N0K0
Last active December 27, 2022 18:35
Show Gist options
  • Save N0K0/5cbdc17a1aff9c5b0c9518f579404daa to your computer and use it in GitHub Desktop.
Save N0K0/5cbdc17a1aff9c5b0c9518f579404daa to your computer and use it in GitHub Desktop.
New York times spelling bee bruteforcer - Tampermonkey
// ==UserScript==
// @name NY Times Spelling Bee Helper
// @namespace http://example.com/
// @version 0.1
// @description A script to help with the NY Times Spelling Bee game
// @author Nikolas "n0k0" Papaioannou
// @match https://www.nytimes.com/puzzles/spelling-bee
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function that takes in a string, and for each letter sends a keydown event, then a keyup event with a delay of 100ms
async function typeString(string) {
console.log(string)
for (let letter of string) {
// Create a KeyboardEvent object for the keypress
window.dispatchEvent(new KeyboardEvent("keydown", {
key: letter,
bubbles: true
}))
await new Promise(resolve => setTimeout(resolve, 1));
// Create a KeyboardEvent object for the key release
window.dispatchEvent(new KeyboardEvent("keyup", {
key: letter,
bubbles: true
}))
// Wait 100ms before typing the next letter
await new Promise(resolve => setTimeout(resolve, 1));
}
window.dispatchEvent(new KeyboardEvent("keydown", {
key: "Enter",
bubbles: true
}))
await new Promise(resolve => setTimeout(resolve, 50));
}
// This function generates all possible combinations of words using the given letters and length
function generateCombinations(letters, length) {
// Base case: if the length is 1, return all single-letter words using the letters
if (length === 1) {
return letters;
}
// Recursive case: generate all combinations of words one length shorter,
// then append each letter to those combinations to get all combinations of the current length
let combinations = [];
for (let combination of generateCombinations(letters, length - 1)) {
for (let letter of letters) {
combinations.push(combination + letter);
}
}
return combinations;
}
// sb-hive-input-content
async function runBruteforcer(letters, letterCenter) {
searching = true;
console.log("Searching")
let chars = [...letters]
chars.push(letterCenter)
let input_dom = document.querySelector(".sb-hive-input")
for (let length = 4; length <= 4; length++) {
// Generate all possible combinations of words of the current length
for (let combination of generateCombinations(chars, length)) {
// Check if the center letter is in the combination
if (combination.includes(letterCenter)) {
await typeString(combination)
}
}
}
}
let letters_found = false;
let searching = false;
let current = 0;
let letters = [];
let center = "";
// Create a div element to hold the detected letters
const debugOverlay = document.createElement('div');
debugOverlay.style.position = 'fixed';
debugOverlay.style.top = '0';
debugOverlay.style.left = '0';
debugOverlay.style.backgroundColor = 'white';
debugOverlay.style.padding = '10px';
debugOverlay.style.border = '1px solid black';
debugOverlay.style.zIndex = '9999';
const debugStatus = document.createElement('div');
debugStatus.style.border = '1px solid black';
const debugLetters = document.createElement('div');
debugLetters.style.border = '1px solid black';
const debugSearch = document.createElement('div');
debugSearch.style.border = '1px solid black';
const controls = document.createElement('div');
debugSearch.style.border = '1px solid black';
const button_start = document.createElement('button');
button_start.innerHTML = "Start!";
button_start.onclick = function(){
// Start the bruteforcer with the detected letters as async
runBruteforcer(letters, center);
}
controls.appendChild(button_start)
debugOverlay.appendChild(debugStatus)
debugOverlay.appendChild(debugLetters)
debugOverlay.appendChild(debugSearch)
debugOverlay.appendChild(controls)
// Append the debug overlay to the body element
document.body.appendChild(debugOverlay);
let statusUpdate = setInterval(() => {
debugStatus.innerHTML = `
<strong>Letters found: :</strong>${letters_found}<br>
<strong>Searching: :</strong>${searching}<br>
<strong>Current: :</strong>${current}<br>
`;
}, 1000);
// Update the debug overlay with the detected letters every 5 seconds
let findLetters = setInterval(() => {
// Find all text elements that are descendants of the element with the ID "sb-hive"
const textElements = document.querySelectorAll('.sb-hive .hive svg text');
console.log(textElements)
// Loop through the text elements and add their innerText to the debug overlay
textElements.forEach(element => {
letters.push(element.textContent)
});
center = letters.shift()
debugLetters.innerHTML = `<strong>Detected letters:</strong><br>${letters}`;
letters_found = true;
clearInterval(findLetters);
}, 250);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment