Wolfar / Battle Monitor For Premium Players

// ==UserScript==
// @namespace    https://openuserjs.org/users/Wolfar
// @name         Battle Monitor For Premium Players
// @version      0.0.0
// @description  Show Online Spectators, Attackers, Defenders On Battles Page Each 5 Sec's
// @include      https://*.e-sim.org/battles.html*
// @license      MIT
// @grant        none
// ==/UserScript==

// ==OpenUserJS==
// @author Wlfar
// ==/OpenUserJs==
var spec = [];
var attack = [];
var defend = [];
var hrefArr = [];
let server = window.location.host.split('.')[1] ? window.location.host.split('.')[0] : false;
let tabletr = document.querySelector('tbody').querySelectorAll('tr');
let battleDiv = document.querySelectorAll('.battleDiv');
for(let i=0; i+1 < tabletr.length;i++) {
    let href = battleDiv[i].childNodes[3].childNodes[3].childNodes[3].href.split('https://' + server + '.e-sim.org/')[1];
    hrefArr.push(href);
    let newDiv = document.createElement('div');
    let newDiv2 = document.createElement('div');
    newDiv2.style = 'width: 100%';
    let newA = document.createElement('p');
    newA.innerHTML = 'Attackers';
    newA.style = 'width: 50%; float:right;';
    attack.push(newA);
    let newD = document.createElement('p');
    newD.innerHTML = 'Defenders';
    newD.style = 'width: 50%; float:left;';
    defend.push(newD);
    let newS = document.createElement('p');
    newS.style = 'text-align:center';
    newS.innerHTML = 'Spectators';
    spec.push(newS);
    let position = tabletr[i+1].childNodes[1].childNodes[1].childNodes[7];
    newDiv.appendChild(newA);
    newDiv.appendChild(newD);
    newDiv2.appendChild(newS);
    newDiv.appendChild(newDiv2);
    position.appendChild(newDiv);
}
function Spectators() {
spec.forEach((x, index) => {
    let y = attack[index];
    let z = defend[index];
    let w = hrefArr[index];
    fetch(w)
    .then(function(response) {
        return response.text()
    })
    .then(function(html) {
        let parser = new DOMParser();
        let doc = parser.parseFromString(html, "text/html");
        let def = doc.querySelector('#totaldefenders').innerHTML;
        let att = doc.querySelector('#totalattackers').innerHTML;
        let All = doc.querySelector('#totalspectators').innerHTML;
        y.innerHTML = '<strong>Attackers: ' + att + '</strong>';
        z.innerHTML = '<strong>Defenders: ' + def + '</strong>';
        x.innerHTML = '<strong>Spectators: ' + All + '</strong>';
    })
    .catch(function(err) {
        console.log('Failed to fetch page: ', err);
})
});
}
var interval = setInterval(function(){Spectators()},5000);