Open in YMusic Button for YouTube

Adds "Open in YMusic" button below YouTube videos.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Open in YMusic Button for YouTube
// @namespace    Violentmonkey Scripts
// @version      1.0
// @description  Adds "Open in YMusic" button below YouTube videos.
// @author       FIRANVAR
// @match        https://www.youtube.com/watch*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  const BUTTON_ID = 'open-in-ymusic-button';

  function createYMusicButton(videoId) {
    const button = document.createElement('a');
    button.id = BUTTON_ID;
    button.textContent = 'Open in YMusic';
    button.href = `https://music.youtube.com/watch?v=${videoId}`;
    button.target = '_blank';
    button.style.display = 'inline-block';
    button.style.marginLeft = '10px';
    button.style.padding = '6px 12px';
    button.style.background = '#ff0000';
    button.style.color = 'white';
    button.style.borderRadius = '4px';
    button.style.textDecoration = 'none';
    button.style.fontWeight = 'bold';
    button.style.fontSize = '14px';
    button.style.cursor = 'pointer';
    return button;
  }

  function getVideoIdFromURL() {
    const urlParams = new URLSearchParams(window.location.search);
    return urlParams.get('v');
  }

  function addButton() {
    const videoId = getVideoIdFromURL();
    if (!videoId) return;

    // Avoid duplicate buttons
    if (document.getElementById(BUTTON_ID)) return;

    // Find the container to insert the button
    const actionButtons = document.querySelector('#top-level-buttons-computed');
    if (actionButtons) {
      const ymusicButton = createYMusicButton(videoId);
      actionButtons.appendChild(ymusicButton);
    }
  }

  // Re-run on navigation (for SPA routing in YouTube)
  const observer = new MutationObserver(() => {
    addButton();
  });

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

  // Initial run
  window.addEventListener('load', addButton);
})();