Nitro Type - Auto Refresh Mobile Trick

Auto Refresh Race by using 2 browser tabs and spoofing as mobile. Doing this preloads the race page and websocket connections.

// ==UserScript==
// @name         Nitro Type - Auto Refresh Mobile Trick
// @version      0.1.0
// @description  Auto Refresh Race by using 2 browser tabs and spoofing as mobile. Doing this preloads the race page and websocket connections.
// @author       Toonidy
// @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//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kTJmTNNZI+caCnPy4OSy5uzyvHEC1yphKAHBt7Daejixfe5P2GtPT4+YpFUQfbUE+2ErqtSoJUS6vzjx8P8/KZqu+huCl+1qhHDyI6Bkloi9ocfpnbPaox2OTrS/PyKMzMJHE8oaITPVacPHZryjTMkIcaXSZtlKiOhac+pFCtbe7jsmri7lOFUQRAKLaFqU7S04gK8NCvrSFDQf7SF+L/YTH3o+hfertB4W63rtAAAAABJRU5ErkJggg==
// @grant        none
// @license      MIT
// @namespace    https://greasyfork.org/users/858426
// @require      https://cdnjs.cloudflare.com/ajax/libs/ismobilejs/0.4.1/isMobile.min.js#sha512-8ZCGLIbRU0hC582pO1934sjq4c7vv9TK+yIluokX5ZNP60xe8PpoHZmn7F457njJSpTJwP7XRuo5LmYLy53n2w==
// @require      https://greasyfork.org/scripts/443718-nitro-type-userscript-utils/code/Nitro%20Type%20Userscript%20Utils.js?version=1042360
// ==/UserScript==

/* global isMobile findReact createLogger */

const logging = createLogger("Nitro Type Auto Refresh Mobile Trick")

if (!isMobile.any) {
    logging.error()("This script only works on mobile devices")
    return
}

////////////
//  Main  //
////////////

const MESSAGE_START = "start",
    MESSAGE_REFRESH = "refresh"

const raceContainer = document.getElementById("raceContainer"),
    raceObj = raceContainer ? findReact(raceContainer) : null,
    server = raceObj?.server,
    currentUserID = raceObj?.props.user.userID

let reloading = false

if (!raceContainer || !raceObj) {
    logging.error("Could not find the race track")
    return
}

/** Message Listener that receives instructions to refresh or start race. */
const channel = new BroadcastChannel("NTAutoRefreshMobileTrick")
channel.onmessage = (e) => {
    if (e.data === MESSAGE_START && raceObj.state.showMobileFocus) {
        raceObj.hideMobileFocus()
    } else if (e.data === MESSAGE_REFRESH) {
        raceObj.raceAgain()
    }
}

/** Track whether starting race. */
server.on("status", (e) => {
    if (e.status === "racing") {
        channel.postMessage(MESSAGE_REFRESH)
    }
})

/** Track Race Finish exact time. */
server.on("update", (e) => {
    const me = e?.racers?.find((r) => r.userID === currentUserID)
    if (me && me.progress.completeStamp > 0 && me.rewards && !reloading) {
        reloading = true
        logging.info()("Refresh Race on alternative tab (finish race acknowledged)")
        channel.postMessage(MESSAGE_START)
    }
})