swiss-badminton extended head 2 head

Shows players comparison not having direct games history through common competitors

当前为 2023-05-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         swiss-badminton extended head 2 head
// @namespace    http://tampermonkey.net/
// @version      1.0-beta.1
// @description  Shows players comparison not having direct games history through common competitors
// @author       all41.dev
// @match        https://www.swiss-badminton.ch/head-2-head*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=swiss-badminton.ch
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const getFetchResp = async (url) => {
        const resp = await fetch(url);
        if (!resp.ok) throw new Error(`fetch failed: ${await resp.text()}`);
        const txt = await resp.text();
        try {
            return JSON.parse(txt);
        } catch (err) {
            return txt;
        }
    }

    const getOptions = async (playerNumber) => {
        const resp = await getFetchResp(`https://www.swiss-badminton.ch/head-2-head/GetPlayerOptions?OrganizationCode=A819E89F-58F3-49B9-9C1F-C865A135F19A&t1p1memberid=${playerNumber}&t1p2memberid=&t2p1memberid=&t2p2memberid=&_=1683628580024`);
        const options = resp.T2P1Options;
        //console.debug(options);
        return options;
    }

    const calcExtH2h = async () => {
        const player1Options = await getOptions(mainUserId);
        //console.debug(player1Options);
        const player2Id = document.querySelector('#player2Id').value;
        //console.debug(player2Id);
        const player2Options = await getOptions(player2Id);
        //console.debug(player2Options);

        const commonOpponents = player1Options.filter((op1) => player2Options.some((op2) => op2.Value === op1.Value));
        console.debug(commonOpponents);
        const container = document.querySelector('#h2h-loading_content');
        container.innerHTML = '';
        const tableContainer = document.createElement('table');
        tableContainer.style.width = '100%';
        tableContainer.innerHTML = '<thead><th></th><th></th></thead><tbody></tbody>';
        container.insertBefore(tableContainer, null);
        const tbodyContainetbodyContainer = tableContainer.querySelector('tbody');
        tbodyContainetbodyContainer.innerHTML = '';

        for(const commonOpp of commonOpponents) {
            console.debug(commonOpp);
            const result = await getMiddlePlayerResults(mainUserId, commonOpp.Value, player2Id);
            const row = document.createElement('tr');
            const left = document.createElement('td');
            left.style.verticalAlign = 'top';
            left.innerHTML = result[0];
            row.insertBefore(left, null);
            const right = document.createElement('td');
            right.style.verticalAlign = 'top';
            right.innerHTML = result[1];
            row.insertBefore(right, null);

            tbodyContainetbodyContainer.insertBefore(row, null);
        }
    }
    const getMiddlePlayerResults = async (mainPlayer, middlePlayer, comparedPlayer) => {
        //                                      https://www.swiss-badminton.ch/head-2-head/Head2HeadContent?OrganizationCode=A819E89F-58F3-49B9-9C1F-C865A135F19A&t1p1memberid=401124&t1p2memberid=&t2p1memberid=403483&t2p2memberid=&_=1683628580027
        const leftResults = await getFetchResp(`https://www.swiss-badminton.ch/head-2-head/Head2HeadContent?OrganizationCode=A819E89F-58F3-49B9-9C1F-C865A135F19A&t1p1memberid=${mainPlayer}&t1p2memberid=&t2p1memberid=${middlePlayer}&t2p2memberid=&_=1683628580027`);
        const rightResults = await getFetchResp(`https://www.swiss-badminton.ch/head-2-head/Head2HeadContent?OrganizationCode=A819E89F-58F3-49B9-9C1F-C865A135F19A&t1p1memberid=${middlePlayer}&t1p2memberid=&t2p1memberid=${comparedPlayer}&t2p2memberid=&_=1683628580027`);

        //console.debug(leftResults);
        //console.debug(rightResults);
        return [leftResults, rightResults];
    }

    const mainUserId = new URLSearchParams(window.location.search).get('T1P1MemberID');

    const titleElem = document.querySelector('h2');
    const extH2HLink = document.createElement('div');
    extH2HLink.style.margins = 'auto';
    extH2HLink.innerHTML = '<input id="player2Id" style="color: black; margins: auto" class="text--xsmall text--center" type="text" value="" placeholder="player #"><button id="extH2hBtn">extended head to head</button><table id="extH2HResult" style="width: 100%"><thead><th>left</th><th>right</th></thead><tbody></tbody></table>';
    titleElem.parentNode.insertBefore(extH2HLink, titleElem.nextSibling);

    document.querySelector('#extH2hBtn').onclick = calcExtH2h;
})();