Neopets: Fix Battledome Loading and Scrolling

Fixes the Battledome's hang on loading when you click, and removes some elements above the battledome so it can be seen in full without scrolling

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name             Neopets: Fix Battledome Loading and Scrolling
// @namespace        kmtxcxjx
// @version          1.1.1
// @description      Fixes the Battledome's hang on loading when you click, and removes some elements above the battledome so it can be seen in full without scrolling
// @match            *://www.neopets.com/dome/arena.phtml
// @grant            none
// @run-at           document-end
// @icon             https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png
// @license          MIT
// ==/UserScript==

(function() {
    'use strict';

    const bd = document.querySelector('div.battledome-container');
    if (!bd) return;

    // This removes some elements from above the battledome, so the battledome can be seen in full without scrolling
    // (or with less scrolling, I guess, depending on screen size)
    Array.from(bd.childNodes).forEach(node => {
        if (
            node.nodeType === Node.TEXT_NODE || // The flavor text above the battledome
            node.nodeName === 'BR' || // Two linebreaks above the battledome
            (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('social-links')) // Social media links above the battledome
        ) {
            node.remove();
        }
    });

    // Detects when the Battledome loading bar gets stuck, and unstucks it by calling window.stop()
    // This is equivalent to pressing the Esc key, which is the usual manual fix for that issue
    let fullTime = null; // when we first saw 100%
    // Checks for the loading bar every 100ms
    const interval = setInterval(() => {
        const bar = bd.querySelector('#loadprogress');

        // Loading bar missing - loading is likely already finished on its own
        if (!bar) return clearInterval(interval);

        const barWidth = parseFloat(bar.style.width) || 0;
        const doneLoading = barWidth >= 28695;
        // The bar is fully loaded for the first time - note the time
        if (doneLoading && fullTime === null) fullTime = performance.now();

        if (fullTime !== null) {
            const elapsed = performance.now() - fullTime;
            if (elapsed >= 200) {
                if (document.contains(bar)) {
                    // 200 or more milliseconds have passed and the loading bar is still there
                    // It's stuck - window.stop() fixes it (equivalent to pressing Esc key)
                    // This listener will call that only once the user clicks, then remove itself
                    document.addEventListener('click', stopOnClick);
                    //window.stop();
                    return clearInterval(interval);
                }
            }
        }
    }, 100);

    function stopOnClick(e) {
        const bar = bd.querySelector('#loadprogress');
        if (bar) window.stop();
        document.removeEventListener('click', stopOnClick);
    }
})();