BCL - Boxscore button

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 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();
  }
})();