BCL - Boxscore button

Přidá tlačítko pod zvolený blok a po kliknutí přejde na #boxscore

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         BCL - Boxscore button
// @namespace    kvido
// @version      1.2
// @description  Přidá tlačítko pod zvolený blok a po kliknutí přejde na #boxscore
// @license      MIT
// @author       JM
// @match        https://www.championsleague.basketball/en/games*
// @match        https://www.championsleague.basketball/en/games/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  const BTN_ID = 'kvido-boxscore-btn';
  const WRAP_ID = 'kvido-boxscore-wrap';
  const TARGET_XPATH = "//*[@id='themeWrapper']/div[2]/div/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]";

  function isGamePage() {
    return /^\/en\/games\/[^/]+/.test(location.pathname);
  }

  function goToBoxscore() {
    const url = new URL(location.href);
    url.hash = 'boxscore';
    location.href = url.toString();
  }

  function getXPathNode(xpath) {
    try {
      return document.evaluate(
        xpath,
        document,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE,
        null
      ).singleNodeValue;
    } catch (e) {
      return null;
    }
  }

  function makeButton() {
    const btn = document.createElement('button');
    btn.id = BTN_ID;
    btn.type = 'button';
    btn.textContent = 'Boxscore';

    Object.assign(btn.style, {
      padding: '10px 14px',
      background: '#111',
      color: '#fff',
      border: 'none',
      borderRadius: '8px',
      cursor: 'pointer',
      fontSize: '14px',
      fontWeight: '700',
      boxShadow: '0 2px 8px rgba(0,0,0,0.25)'
    });

    btn.addEventListener('click', goToBoxscore);
    return btn;
  }

  function ensureButton() {
    if (!isGamePage()) return;
    if (!document.body) return;

    const target = getXPathNode(TARGET_XPATH);
    if (!target) return;

    let wrap = document.getElementById(WRAP_ID);

    if (!wrap) {
      wrap = document.createElement('div');
      wrap.id = WRAP_ID;

      Object.assign(wrap.style, {
        marginTop: '10px',
        display: 'flex',
        justifyContent: 'flex-start',
        alignItems: 'center'
      });

      wrap.appendChild(makeButton());
      target.insertAdjacentElement('afterend', wrap);
      return;
    }

    if (!document.getElementById(BTN_ID)) {
      wrap.appendChild(makeButton());
    }

    if (wrap.previousElementSibling !== target) {
      target.insertAdjacentElement('afterend', wrap);
    }
  }

  function init() {
    ensureButton();

    const observer = new MutationObserver(() => {
      ensureButton();
    });

    observer.observe(document.documentElement, {
      childList: true,
      subtree: true
    });

    setInterval(ensureButton, 1500);
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();