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.

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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();
  }
})();