Shazam My Library Audio Downloader

Adds download buttons next to tracks in Shazam My Library for audio previews.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Shazam My Library Audio Downloader
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Adds download buttons next to tracks in Shazam My Library for audio previews. 
// @author       Jeffrey
// @match        https://www.shazam.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    console.log('Script is running...');

    // Function to add the download button
    function addDownloadButtons() {
        // Find all track items that have the audio URL
        let trackElements = document.querySelectorAll('article[data-shz-audio-url]');
        console.log(`Found ${trackElements.length} tracks.`);

        trackElements.forEach(track => {
            // Ensure no duplicate button
            if (!track.querySelector('.download-button')) {
                // Create the download button
                let downloadButton = document.createElement('a');
                downloadButton.textContent = '⬇️ Download Preview';
                downloadButton.classList.add('download-button');
                downloadButton.style.display = 'inline-block';  // Ensure the button is displayed
                downloadButton.style.marginLeft = '10px';
                downloadButton.style.cursor = 'pointer';
                downloadButton.style.fontSize = '14px';
                downloadButton.style.color = '#007bff';  // Make the button blue and visible
                downloadButton.style.textDecoration = 'none'; // Remove underline
                downloadButton.style.fontWeight = 'bold'; // Make it more prominent

                // Get the audio preview URL from the element's data attribute
                let previewUrl = track.getAttribute('data-shz-audio-url');
                console.log('Preview URL:', previewUrl);

                // Set the download link attributes
                if (previewUrl) {
                    downloadButton.href = previewUrl;
                    downloadButton.download = 'audio-preview.m4a';  // Name the file accordingly
                }

                // Insert the button into the track details area (append to the last div in details)
                let trackDetails = track.querySelector('.details');
                if (trackDetails) {
                    // Try appending it in a more prominent position
                    trackDetails.appendChild(downloadButton);
                    console.log('Download button added.');
                }
            }
        });
    }

    // Observe changes in the DOM to detect when the tracks are loaded
    const observer = new MutationObserver((mutationsList, observer) => {
        const trackElements = document.querySelectorAll('article[data-shz-audio-url]');
        if (trackElements.length > 0) {
            console.log('Tracks detected, adding download buttons...');
            addDownloadButtons();
            observer.disconnect(); // Stop observing once tracks are found and buttons are added
        }
    });

    // Start observing the page for changes in the child elements of the body
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

})();