Last active
July 7, 2021 01:52
-
-
Save beaulac/6611fc1f0a7a88e0481fb103998a4eee to your computer and use it in GitHub Desktop.
NYT Spelling Bee QOL UserScript
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 NYT Spelling Bee QOL | |
// @namespace http://tampermonkey.net/ | |
// @source https://gist.github.com/beaulac/6611fc1f0a7a88e0481fb103998a4eee | |
// @version 0.2 | |
// @description Small NYTimes spelling bee improvements; rough WIP | |
// @author github.com/beaulac | |
// @match https://www.nytimes.com/puzzles/spelling-bee | |
// @icon https://www.google.com/s2/favicons?domain=nytimes.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const gameData = window.gameData.today; | |
const { answers, pangrams, id: puzzleId } = gameData; | |
function calculatePoints() { | |
const sum = pts => pts.reduce((acc, pt) => acc + pt, 0); | |
const wordValue = w => w.length === 4 ? 1 : w.length; | |
const pangramValue = w => wordValue(w) + 7; | |
const nonPangrams = answers.filter(w => !pangrams.includes(w)); | |
return sum([...nonPangrams.map(wordValue), ...pangrams.map(pangramValue)]); | |
} | |
async function getFoundWords() { | |
const userToken = window.sessionStorage.getItem('pz-user-check'); | |
const url = `https://nyt-games-prd.appspot.com/svc/spelling-bee/v1/game/${puzzleId}.json`; | |
const response = await fetch(url, { headers: { 'nyt-s': userToken } }); | |
const data = await response.json(); | |
return data.answers; | |
} | |
async function getMissingWords() { | |
const foundWords = await getFoundWords(); | |
return answers.filter(a => !foundWords.includes(a)); | |
} | |
async function showMissingWords() { | |
const missingWords = await getMissingWords(); | |
const insertBeforeNode = document.querySelector('div.sb-content-box'); | |
const containerNode = document.createElement('div'); | |
containerNode.style = 'text-align: center'; | |
const listNode = document.createElement('ul'); | |
listNode.style = 'font-size: 1.2em; line-height: 1.5em'; | |
containerNode.appendChild(listNode); | |
missingWords.map( | |
word => listNode.appendChild( | |
Object.assign( | |
document.createElement('li'), | |
{ innerHTML: `<a style="text-decoration: underline" href="https://www.google.com/search?q=define%3A${word}" target="_define${word}">${word}</a>` } | |
) | |
) | |
); | |
insertBeforeNode.parentNode.insertBefore(containerNode, insertBeforeNode); | |
} | |
function createPointsSpan() { | |
const totalPoints = calculatePoints(); | |
// Genius points are 70% total points, rounded. | |
const points = Math.round(0.7 * totalPoints); | |
const pointsSpan = document.createElement('span'); | |
pointsSpan.style = 'display: inline-block; line-height: 45px;'; | |
pointsSpan.innerHTML = `Genius Points: ${points}<br/>Total Points: ${totalPoints}`; | |
return pointsSpan; | |
} | |
function createMissingWordsLink() { | |
return Object.assign( | |
document.createElement('a'), | |
{ | |
href: '#', | |
target: 'spellingBeeAnswers', | |
style: 'display: inline-block; line-height: 45px;', | |
innerText: 'Show missing words', | |
onclick: (e) => { | |
e.preventDefault(); | |
showMissingWords(); | |
} | |
} | |
); | |
} | |
function createPointsElement() { | |
const yesterdaysAnswersNode = document.querySelector('span.pz-toolbar-button.pz-toolbar-button__yesterday'); | |
if (yesterdaysAnswersNode) { | |
const pointsSpan = createPointsSpan(); | |
yesterdaysAnswersNode.parentNode.insertBefore(pointsSpan, yesterdaysAnswersNode); | |
const link = createMissingWordsLink(); | |
yesterdaysAnswersNode.parentNode.insertBefore(link, yesterdaysAnswersNode); | |
} | |
console.warn('Could not find element to insert'); | |
} | |
createPointsElement(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment