Race Result Points Nitro Type

Shows Points earned from the race.

// ==UserScript==
// @name         Race Result Points Nitro Type
// @version      0.1.1
// @description  Shows Points earned from the race.
// @author       Nate Dogg
// @match        *://*.nitrotype.com/race
// @match        *://*.nitrotype.com/race/*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAIAAACR5s1WAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9bpVIqilYQcchQnSyIijhqFYpQIdQKrTqYXPoFTRqSFBdHwbXg4Mdi1cHFWVcHV0EQ/ABxc3NSdJES/5cUWsR4cNyPd/ced+8Af73MVLNjHFA1y0gl4kImuyoEXxFCL/ogYEBipj4nikl4jq97+Ph6F+NZ3uf+HN1KzmSATyCeZbphEW8QT29aOud94ggrSgrxOfGYQRckfuS67PIb54LDfp4ZMdKpeeIIsVBoY7mNWdFQiaeIo4qqUb4/47LCeYuzWq6y5j35C8M5bWWZ6zSHkcAiliBSRzKqKKEMCzFaNVJMpGg/7uEfcvwiuWRylcDIsYAKVEiOH/wPfndr5icn3KRwHOh8se2PESC4CzRqtv19bNuNEyDwDFxpLX+lDsx8kl5radEjoGcbuLhuafIecLkDDD7pkiE5UoCmP58H3s/om7JA/y0QWnN7a+7j9AFIU1fJG+DgEBgtUPa6x7u72nv790yzvx9fO3KfqkKlgwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+ULCBEQCo/KC2cAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAABmJLR0QAcgACAAKX272SAAAHfklEQVRYw82YCVATVxjHX2mr1tLSghTUaqFY7CFqK9RjqLWdsdfYqaP2omU6bUdbZ7D1KkjAOkWtNIBQETk8KAiCEGQUuSyHIQQJLLnI5lpCyLUhJNyIgJX04eIzkihEpHbnP5D98vJ9v/3e977dfcAcH//QBah/Ox0cHopuQzwsAsQxCpHx/IL706k5ninuLyBNCuKi12viJasmKZ7P8klBhE2fyfdZMUmIC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SoJUS6vzjx8P8/KZqu+huCl+1qhHDyI6Bkloi9ocfpnbPaox2OTrS/PyKMzMJHE8oaITPVacPHZryjTMkIcaXSZtlKiOhac+pFCtbe7jsmri7lOFUQRAKLaFqU7S04gK8NCvrSFDQf7SF+L/YTH3o+hfertB4W63rtAAAAABJRU5ErkJggg==
// @grant        none
// @namespace https://greasyfork.org/users/805959
// ==/UserScript==

/////////////
//  Utils  //
/////////////

/** Finds the React Component from given dom. */
const findReact = (dom, traverseUp = 0) => {
	const key = Object.keys(dom).find((key) => key.startsWith("__reactFiber$"))
	const domFiber = dom[key]
	if (domFiber == null) return null
	const getCompFiber = (fiber) => {
		let parentFiber = fiber?.return
		while (typeof parentFiber?.type == "string") {
			parentFiber = parentFiber?.return
		}
		return parentFiber
	}
	let compFiber = getCompFiber(domFiber)
	for (let i = 0; i < traverseUp && compFiber; i++) {
		compFiber = getCompFiber(compFiber)
	}
	return compFiber?.stateNode
}

/** Console logging with some prefixing. */
const logging = (() => {
	const logPrefix = (prefix = "") => {
		const formatMessage = `%c[Nitro Type Race Result Points]${prefix ? `%c[${prefix}]` : ""}`
		let args = [console, `${formatMessage}%c`, "background-color: #D62F3A; color: #fff; font-weight: bold"]
		if (prefix) {
			args = args.concat("background-color: #4f505e; color: #fff; font-weight: bold")
		}
		return args.concat("color: unset")
	}
	return {
		info: (prefix) => Function.prototype.bind.apply(console.info, logPrefix(prefix)),
		warn: (prefix) => Function.prototype.bind.apply(console.warn, logPrefix(prefix)),
		error: (prefix) => Function.prototype.bind.apply(console.error, logPrefix(prefix)),
		log: (prefix) => Function.prototype.bind.apply(console.log, logPrefix(prefix)),
		debug: (prefix) => Function.prototype.bind.apply(console.debug, logPrefix(prefix)),
	}
})()

/////////////
//  Utils  //
/////////////

const raceContainer = document.getElementById("raceContainer"),
	raceObj = raceContainer ? findReact(raceContainer) : null
if (!raceContainer || !raceObj) {
	logging.error("Init")("Could not find the race track")
	return
}

const currentUserID = raceObj.props.user.userID

/** Mutation obverser to track whether results screen showed up. */
const resultObserver = new MutationObserver(([mutation], observer) => {
	for (const newNode of mutation.addedNodes) {
		if (newNode.classList.contains("race-results")) {
			observer.disconnect()

			const currentUserResult = raceObj.state.racers.find((r) => r.userID === currentUserID)
			if (!currentUserResult || !currentUserResult.progress || typeof currentUserResult.place === "undefined") {
				logging.warn("Finish")("Unable to find race results")
				return
			}

			const listItemTarget = document.querySelector(
				"#raceContainer .gridTable-row.is-self .list .list-item:last-of-type"
			)
			if (!listItemTarget) {
				logging.warn("Finish")("Unable to update user's race result")
				return
			}

			const { typed, skipped, startStamp, completeStamp, errors } = currentUserResult.progress,
				wpm = Math.round((typed - skipped) / 5 / ((completeStamp - startStamp) / 6e4)),
				acc = ((1 - errors / (typed - skipped)) * 100).toFixed(2),
				points = Math.round((100 + wpm / 2) * (1 - errors / typed))

			const listContainer = listItemTarget.parentNode,
				listItemPoints = document.createElement("div")

			listItemPoints.classList.add("list-item")
			listItemPoints.innerHTML = `${points} <span class="${
				listItemTarget.querySelector("span")?.className || "tc-ts"
			}">Points</span>`
			listContainer.insertBefore(listItemPoints, listItemTarget)

			logging.info("Finish")(`Points collected ${points}`)
			break
		}
	}
})

resultObserver.observe(raceContainer, { childList: true })

logging.info("Init")("Race Result listener has been setup")