Shazam YouTube Search Button for track pages

Adds a YouTube search button to Shazam track pages next to track name

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Shazam YouTube Search Button for track pages
// @namespace   Violentmonkey Scripts
// @match       https://www.shazam.com/song/*
// @grant       none
// @version     1.0
// @author      raefraem
// @description Adds a YouTube search button to Shazam track pages next to track name
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  // Function to create YouTube search URL
  function createYouTubeSearchURL(track, artist) {
    const searchQuery = encodeURIComponent(`${track} - ${artist}`);
    return `https://www.youtube.com/results?search_query=${searchQuery}`;
  }

  // Function to create YouTube button
  function createYouTubeButton(track, artist) {
    const button = document.createElement('button');
    button.innerHTML = '▶ YouTube';
    button.style.cssText = `
            background-color: #FF0000;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 5px 10px;
            margin-left: 10px;
            cursor: pointer;
            font-size: 12px;
            vertical-align: middle;
        `;

    button.addEventListener('click', e => {
      // Stop the event from bubbling up and triggering Shazam's click handler
      e.preventDefault();
      e.stopPropagation();

      // Open YouTube in a new tab
      window.open(createYouTubeSearchURL(track, artist), '_blank');

      // Return false to ensure the event is completely stopped
      return false;
    });

    return button;
  }

  function addYouTubeButton() {
    const artistEl = document.querySelector('.TrackPageHeader_songDetail__I618J h1');
    const artist = artistEl.textContent;
    const track = document.querySelector('.TrackPageHeader_songDetail__I618J h2').textContent;

    console.log('artist', artist, 'track', track);

    const youtubeButton = createYouTubeButton(track, artist);
    artistEl.appendChild(youtubeButton);
  }

  setTimeout(addYouTubeButton, 300);
})();