Nexus Clash Improved Character Select (B4)

Adds colored resource bars and highlights characters according to certain conditions

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Nexus Clash Improved Character Select (B4)
// @namespace    https://roadha.us
// @version      0.2
// @description  Adds colored resource bars and highlights characters according to certain conditions
// @author       haliphax
// @match        https://www.nexusclash.com/modules.php?name=Game*
// ==/UserScript==

(function() {
    'use strict';

    let headers = document.querySelectorAll('center > h2'),
        isSelectScreen = false;

    for (let i = 0; i < headers.length; i++) {
        if (headers[i].innerText.trim().startsWith('Welcome')) {
            isSelectScreen = true;
            break;
        }
    }

    if (!isSelectScreen) return;

    let columnHeaders = document.querySelectorAll('th');
    var characterTable = null;

    for (let i = 0; i < columnHeaders.length; i++) {
        if (columnHeaders[i].innerText.trim() == 'Character') {
            characterTable = columnHeaders[i].parentNode.parentNode.parentNode;
            break;
        }
    }

    if (characterTable === null) {
        console.warn('Could not find character table');
        return;
    }

    let characters = Array.from(characterTable.children[0].children)
        .filter((el, idx, arr) => idx > 0 && idx < arr.length - 1);

    for (let i = 0; i < characters.length; i++) {
        let row = characters[i],
            apCell = row.children[2],
            apString = apCell.innerText.trim(),
            apSplit = apString.indexOf('/'),
            hpCell = row.children[3],
            hpString = hpCell.innerText.trim(),
            hpSplit = hpString.indexOf('/'),
            mpCell = row.children[4],
            mpString = mpCell.innerText.trim(),
            mpSplit = mpString.indexOf('/'),
            char = {
                ap: apString.substring(0, apSplit),
                apMax: apString.substring(apSplit + 1),
                hp: hpString.substring(0, hpSplit),
                hpMax: hpString.substring(hpSplit + 1),
                mp: mpString.substring(0, mpSplit),
                mpMax: mpString.substring(mpSplit + 1)
            };

        [apCell, hpCell, mpCell].forEach((cell) => {
            let img = null,
                op1 = null,
                op2 = null;

            switch (cell) {
                case apCell:
                    img = 'ap';
                    op1 = char.ap;
                    op2 = char.apMax;
                    break;
                case hpCell:
                    img = 'hp';
                    op1 = char.hp;
                    op2 = char.hpMax;
                    break;
                case mpCell:
                    img = 'mp';
                    op1 = char.mp;
                    op2 = char.mpMax;
                    break;
            }

            let pct = Math.min(100, Math.max(0, op1 / op2 * 100));

            cell.classList.add('stat-bar');

            if (pct === 0) {
                cell.classList.add('empty');
            }
            else {
                cell.style = 'background-size: ' + pct + '% 100%; background-image: url(/images/g/' + img + '-bar.png);';

                if (pct === 100) cell.classList.add('full');
            }
        });
    }

    let css = document.createElement('style');

    css.innerText = `
        .stat-bar { background-repeat: no-repeat; background-position: left top; text-align: center; padding: .5em; }
        .stat-bar.full { font-weight: bold; }
        .stat-bar.empty { color: #900; font-weight: bold; }
    `;
    document.head.appendChild(css);
})();