Bonk Speedrun Timer

Adds a speedrun timer.

目前為 2022-11-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Bonk Speedrun Timer
// @version      2.0.1
// @author       Fury_101
// @description  Adds a speedrun timer.
// @match        https://bonk.io/gameframe-release.html
// @run-at       document-end
// @grant        none
// @license      GNU GPLv3
// @namespace    https://greasyfork.org/users/919958
// ==/UserScript==

const container = document.createElement("div");
document.getElementById('pagecontainer').appendChild(container);
container.outerHTML = `
<div class="windowShadow" style="display: unset;" id="BST__container">
    <div class="newbonklobby_boxtop newbonklobby_boxtop_classic" id="BST__top" style="background-color: #009688;">
        BST
    </div>
	<div id="BST__timercontainer" style="text-align:center">
        <span class="BST__time" id="BST__currtime">00:00.00</span>
    </div>
    <div id="BST__container-btm">
	    <div class="newbonklobby_settings_button brownButton brownButton_classic buttonShadow" style="width:100%;" id="BST__hidetimer">
	    	Hide Timer
        </div>
        <div>
          <input type="range" id="BST__speed" name="BST__speed" step="0.1" min="0.5" max="1.5" value="1" oninput="this.nextElementSibling.value = this.value">
          <output>1</output>
          <label for="BST__speed">Speed</label>
        </div>
    </div>
</div>
`

document.getElementById("BST__hidetimer").addEventListener("click", (e) => {
    if (document.getElementById("BST__container").style.visibility === "hidden") {
        document.getElementById('BST__container').style.visibility = "inherit"; //shows the container
        document.getElementById("BST__container").style.height="auto";
        e.target.style.width='100%';
        e.target.innerHTML='Hide Timer';
        return;
    }
    document.getElementById("BST__container").style.height="30px"; //hides the container
    document.getElementById("BST__container").style.visibility = "hidden";
    e.target.style.width='25%';
    e.target.style.margin="0 auto";
    e.target.innerHTML='Show';
});

let BSTCSS = document.createElement('style');
BSTCSS.innerHTML = `
#BST__container {
    text-align: center;
    font-family: "futurept_b1";
    background-color: #cfd8cd;
    width: calc(35.2vw - 400px);
    min-width: 154px;
    max-width: 260px;
    position: absolute;
    height: auto;
    right: 1%;
    top: 60px;
    border-radius: 7px;
    transition: ease-in-out 100ms;
    z-index: 10;
}
.BST__time {
    height: 32px;
    line-height: 32px;
    font-size: 20px;
    display: block;
}
.BST__split {
    color: blue;
}
#BST__currtime {
    margin-bottom: calc(100px);
}
#BST__container-btm {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}
#BST__hidetimer {
    visibility: visible;
}
#BST__container label {
    width: 100%;
    text-align: center;
}
`
document.getElementsByTagName('head')[0].appendChild(BSTCSS);

const credit = document.getElementById("ingamemapcredit");
const countdown = document.getElementById("ingamecountdown");
const winner = document.getElementById("ingamewinner");
const lobby = document.getElementById("newbonklobby");
const config = {
    attributes: true,
    attributeOldValue: true,
    attributeFilter: ["style"]
}

window.startTime = null;
window.timerStarted = false;

const startObserver = new MutationObserver(mutationList => { //checks if the countdown or map credit screen goes away
    for (const mutation of mutationList) {
        if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
            if (mutation.target.style.visibility === "hidden" && mutation.oldValue.search("visibility: inherit") !== -1) {
                console.log("START TIMER");
                [...document.querySelectorAll(".BST__split")].map(e => {e.remove()});
                window.startTime = Date.now();
                window.timerStarted = true;
                break;
            }
        }
    }
});

startObserver.observe(countdown, config);
startObserver.observe(credit, config);

const endObserver = new MutationObserver(mutationList => { //checks if the winning screen shows up
    for (const mutation of mutationList) {
        if (mutation.type === 'attributes' && mutation.attributeName === 'style' && window.timerStarted) {
            if (mutation.target.style.visibility === "inherit" && mutation.oldValue.search("visibility: hidden") !== -1) {
                console.log("END TIMER");
                window.timerStarted = false;
                console.log(`FINAL TIME: ${Date.now() - window.startTime}`);
            }
        }
    }
});

endObserver.observe(winner, config);

const checkObserver = new MutationObserver(mutationList => { //checks if the lobby shows up as a precaution
    for (const mutation of mutationList) {
        if (mutation.type === 'attributes' && mutation.attributeName === 'style' && window.timerStarted) {
            if (mutation.target.style.display === "block" && mutation.oldValue.search("display: none;") !== -1) {
                console.log("END TIMER");
                window.timerStarted = false;
                console.log(`FINAL TIME: ${Date.now() - window.startTime}`);
            }
        }
    }
});

checkObserver.observe(lobby, config);

window.onload = e => {
    document.addEventListener("keydown", e => {
        if (e.target.nodeName === "INPUT") return;
        if (e.key === "r")
            document.getElementById("hostPlayerMenuRestartButton").click();
        if (e.key === "t" && window.timerStarted) {
            const now = new Date(Date.now() - window.startTime)
            const span = document.createElement("span");
            span.innerText = `${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}.${now.getMilliseconds().toString().padStart(2, '0')}`;
            span.classList.add("BST__time", "BST__split");
            document.getElementById("BST__timercontainer").insertBefore(span, document.getElementById("BST__currtime"));
        }
    });
}

function injector(src){
    let newSrc = src;

    //updates timer every frame
    const replaceMents = [
        {
            position: "{V6D[67][h1k[9][1121]][h1k[9][1660]]();}",
            value: "if(window?.timerStarted){const now=new Date(Date.now()-window.startTime);document.getElementById('BST__currtime').innerText=`${now.getMinutes().toString().padStart(2,'0')}:${now.getSeconds().toString().padStart(2,'0')}.${now.getMilliseconds().toString().padStart(2,'0')}`}"
        },
        {
            position: "Q[V6D[3][129]][V6D[3][128]]=function(h4w,d5Z,y9B,l4e,l1V,N69,j0P,H9G){",
            value: `arguments[3] = 30 / document.getElementById("BST__speed").value;`
        }
    ]

    replaceMents.forEach(e => {newSrc = newSrc.replace(e.position, e.position + e.value)});

    if(src === newSrc) throw "Injection failed!";
    console.log("Bonk Speedrun Timer injector run");
    return newSrc;
}

// Compatibility with Excigma's code injector userscript
if(!window.bonkCodeInjectors) window.bonkCodeInjectors = [];
window.bonkCodeInjectors.push(bonkCode => {
    try {
        return injector(bonkCode);
    } catch (error) {
        alert(`Whoops! Bonk Speedrun Timer was unable to load.
This may be due to an update to Bonk.io. If so, please report this error!
This could also be because you have an extension that is incompatible with \
Bonk Speedrun Timer`);
        throw error;
    }
});

console.log("Bonk Speedrun Timer injector loaded");