Add Spotify and YouTube Buttons to Shazam

Adds Spotify and YouTube buttons to Shazam track pages

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Add Spotify and YouTube Buttons to Shazam
// @author       tanso
// @description  Adds Spotify and YouTube buttons to Shazam track pages
// @version      1.1
// @license      MIT
// @match        https://www.shazam.com/*
// @grant        none
// @namespace    https://greasyfork.org/users/1467885
// ==/UserScript==
 
(() => {
    'use strict';

    const addSpotifyButton = () => {
        const shareButton = document.querySelector('.TrackPageHeader_share__q1WWs');
        if (!shareButton) return;

        const spotifyButton = shareButton.cloneNode(true);
        const spotifyButtonLink = spotifyButton.querySelector('a');

        spotifyButtonLink.innerHTML = `<span class="Button-module_label__k1Dkf">
            <img src="https://storage.googleapis.com/pr-newsroom-wp/1/2023/05/Spotify_Primary_Logo_RGB_Green.png"
            style="width: 40px; height: 40px; margin-right: 15px; vertical-align: middle;">Spotify</span>`;

        spotifyButtonLink.style.padding = "7px 30px 7px 7px";
        spotifyButtonLink.querySelector('.Button-module_label__k1Dkf').style.cssText += 'color: white !important;';
        spotifyButtonLink.style.backgroundColor = '#101218';

        const songTitle = document.querySelector('.TrackPageHeader_title__wGI_Q')?.textContent;
        const artistName = document.querySelector('meta[itemprop="name"]')?.content;
        if (!songTitle || !artistName) return;

        const encodedSearch = encodeURIComponent(`${artistName} ${songTitle}`);

        spotifyButtonLink.href = `https://open.spotify.com/search/${encodedSearch}`;
        spotifyButtonLink.target = '_blank';

        shareButton.parentNode.insertBefore(spotifyButton, shareButton);
    };

    const addYouTubeButton = () => {
        const shareButton = document.querySelector('.TrackPageHeader_share__q1WWs');
        if (!shareButton) return;

        const youtubeButton = shareButton.cloneNode(true);
        const youtubeButtonLink = youtubeButton.querySelector('a');

        youtubeButtonLink.innerHTML = `<span class="Button-module_label__k1Dkf">
            <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/YouTube_icon_%282013-2017%29.png/800px-YouTube_icon_%282013-2017%29.png"
            style="width: 40px; height: 40px; margin-right: 15px; vertical-align: middle; border-radius: 50%;">YouTube</span>`;

        youtubeButtonLink.style.padding = "7px 30px 7px 7px";
        youtubeButtonLink.querySelector('.Button-module_label__k1Dkf').style.cssText += 'color: white !important;';
        youtubeButtonLink.style.backgroundColor = '#2E2E2E';

        const songTitle = document.querySelector('.TrackPageHeader_title__wGI_Q')?.textContent;
        const artistName = document.querySelector('meta[itemprop="name"]')?.content;
        if (!songTitle || !artistName) return;

        const encodedSearch = encodeURIComponent(`${artistName} ${songTitle}`);

        youtubeButtonLink.href = `https://www.youtube.com/results?search_query=${encodedSearch}`;
        youtubeButtonLink.target = '_blank';

        shareButton.parentNode.insertBefore(youtubeButton, shareButton.nextSibling);
    };

    window.addEventListener('load', () => {
        addSpotifyButton();
        addYouTubeButton();
    });
})();