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