Autodarts - Show thrown darts each leg in stats

Zeigt die Statistik besser an.

// ==UserScript==
// @name         Autodarts - Show thrown darts each leg in stats
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Zeigt die Statistik besser an.
// @author       benebelter
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @match        https://play.autodarts.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=autodarts.io
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM_xmlhttpRequest
// @license      MIT
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    var legsoverview  = new Array();
    let table_head = '';
    let table_rows = '';
    var shortlegs = new Array();
    var players = new Array();
    var interval = setInterval(function() {


        if( $(".ad-ext-player-name").length != 0
           && window.location.href.indexOf("/history/matches/") != -1 ) {


            if(   $("#dartsthrows").length === 0 ) {
                var gameid =  location.pathname.split("/")[3] ;

            fetch('https://api.autodarts.io/as/v0/matches/'+gameid+'/stats', {
                credentials: 'include',
                method: 'GET'
            })
                .then(function(response) { return response.json(); })
                .then(function(json) {

                for (let i = 0; i < json['games'].length; i++){

                    if(i == 0) {// collect players
                        for (let p = 0; p < json['players'].length; p++) {

                            // https://play.autodarts.io/history/matches/3ddf570c-d45e-46ed-9a56-e19a82881772
                            players.push  ({'id':p , 'name': json['players'][p]['name'] , 'playerid': json['players'][p]['id'] });
                            console.log( players[p] );
                            var td = td + '<td class="css-1fq7vy1" id="td_player_'+p+'"></td>';
                        }

                        // find html & add row to page
                        let append = '<tr class="css-0" id="dartsthrows"><td class="css-1vmvwla">Darts legs won</td>'+td+'</tr>';
                        console.log ('dartsthrow length: '+$('#dartsthrows').length);
                        if($('#dartsthrows').length == 0) {
                            $('.chakra-table > thead ').after(append); }


                    }

                    var winner = players.find(item => item.playerid === json['games'][i]['winnerPlayerId']);
                    var winner_name = winner['name'];
                    var winner_id = winner['id'];
                    //console.log('Leg '+i+' winner_name: '+ winner_name);
                    //console.log('winner_id: '+ winner_id);

                    ////////////// Darts geworfen
                    // welcher Spieler ist der Winner im aktuellen Leg gewesen?
                    let winnerindex = json['legStats'][i]['playerIndices'].indexOf(winner_id);
                    let dartsthrown = json['legStats'][i]['stats'][winnerindex]['dartsThrown'];

                    // Restscore other players this leg
                    for (let p = 0; p < json['players'].length; p++) {
                        var name = players.find(item => item.id === p);
                        table_rows += '<tr><td>'+name['name']+'</td>';
                        for (let i = 0; i < json['games'].length; i++){
                            table_rows += '<td>15d</td>';}
                        table_rows += '</tr>';


                        if( p == winnerindex) {var iswinner = 1;} else {var iswinner = 0;}
                        legsoverview.push  ( {'leg':i , 'playerid':p ,
                                              'leftscore': json['games'][i]['scores'][p],
                                              'iswinner': iswinner ,
                                              'dartstrown':json['legStats'][i]['stats'][p]['dartsThrown']
                                             } );
                    }
                    //bould table
                    table_head = '<table><tr class=""><th class="" style="min-width: 40px; border-bottom: 1px solid #c0c0c0; border-right: 1px solid #c0c0c0;  ">Leg</th>';
                    for (let i = 0; i < json['games'].length; i++){
                        table_head += '<th class="text-center" style="min-width: 40px; border-bottom: 1px solid #c0c0c0; border-right: 1px solid #c0c0c0;  ">'+(i+1)+'</th>';
                    }

                    table_head += table_rows+'</tr></table>';



                    $('#td_player_'+winner_id).append('<span style="white-space:nowrap; background-color: #4162a1; color: white; border-radius: 10% / 50%; padding: 2px 5px 2px 5px; margin-left: 2px;">'+dartsthrown+'</span>');


                }

            }); // end-fetch

            } // end if $("#dartsthrows").length
        } // end if $(".ad-ext-player-name").length != 0 
        //$('.chakra-table > thead ').after(table_head);
    //    console.log('table_output ; '+table_head);
    }, 2000)


    })();