swiss-badminton extended head 2 head

Shows players comparison not having direct games history through common competitors

目前為 2023-05-10 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         swiss-badminton extended head 2 head
// @namespace    http://tampermonkey.net/
// @version      1.0-beta.0
// @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="401465"><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;
})();