Open in YMusic Button for YouTube

Adds "Open in YMusic" button below YouTube videos.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         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);
})();