Greasy Fork 支持简体中文。

Uppbeat Downloader

Enables the download button functionality.

// ==UserScript==
// @name         Uppbeat Downloader
// @description  Enables the download button functionality.
// @icon         https://uppbeat.io/favicon.ico
// @version      1.0
// @author       afkarxyz
// @namespace    https://github.com/afkarxyz/misc-scripts/
// @supportURL   https://github.com/afkarxyz/misc-scripts/issues
// @license      MIT
// @match        https://uppbeat.io/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function setupDownloadHandler() {
        const observer = new PerformanceObserver((list) => {
            list.getEntries().forEach((entry) => {
                if (entry.name.toLowerCase().endsWith('.mp3')) {
                    handleDownload(entry.name);
                }
            });
        });
        
        observer.observe({ entryTypes: ['resource'] });

        document.querySelectorAll('button[type="button"] img[alt="Download"]').forEach(downloadBtn => {
            const button = downloadBtn.closest('button');
            const oldButton = button.cloneNode(true);
            button.parentNode.replaceChild(oldButton, button);
            
            oldButton.addEventListener('click', (e) => {
                e.preventDefault();
                e.stopPropagation();
                handleDownloadClick(e);
            });
        });
    }

    function handleDownloadClick(event) {
        const trackElement = event.target.closest('[data-testid="track-player-track"]');
        if (!trackElement) return;

        const playButton = trackElement.querySelector('[data-testid="play-pause-button-paused"]');
        if (playButton) {
            playButton.click();
        }

        const trackName = trackElement.querySelector('[data-testid="track-name"]')?.textContent.trim();
        const artistName = trackElement.querySelector('.ub-track-info-subtitle')?.textContent.trim();
        
        window.currentDownloadInfo = {
            trackName,
            artistName
        };
    }

    function handleDownload(audioUrl) {
        if (!window.currentDownloadInfo) return;
        
        const { trackName, artistName } = window.currentDownloadInfo;
        const fileName = `${artistName} - ${trackName}.mp3`;

        fetch(audioUrl)
            .then(response => response.blob())
            .then(blob => {
                const blobUrl = window.URL.createObjectURL(blob);
                const downloadLink = document.createElement('a');
                downloadLink.style.display = 'none';
                downloadLink.href = blobUrl;
                downloadLink.download = fileName;
                
                document.body.appendChild(downloadLink);
                downloadLink.click();
                
                setTimeout(() => {
                    document.body.removeChild(downloadLink);
                    window.URL.revokeObjectURL(blobUrl);
                }, 100);
            });
        
        window.currentDownloadInfo = null;
    }

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