SampleFocus Downloader [fixed lol]

original samplefocus downloader some devs are lazy so i made my own

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         SampleFocus Downloader [fixed lol]
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  original samplefocus downloader some devs are lazy so i made my own
// @author       CSI JML
// @match        https://samplefocus.com/samples/*
// @grant        GM_download
// @license      MIT
// @run-at       document-end
// ==/UserScript==

// i formatted it nicely so anyone who is suspicious of this script can read it easily :p

(function() {
    'use strict';

    function injectDownloadButton() {
        const heroSection = document.querySelector('.section.sample-hero');
        if (!heroSection) return;

        const originalBtnContainer = document.querySelector('.css-116gh75');
        if (!originalBtnContainer || document.getElementById('quick-mp3-download')) return;

        let mp3Url = "";

        const scripts = document.querySelectorAll('script');
        for (let s of scripts) {
            const match = s.textContent.match(/"sample_mp3_url":"(https:\/\/[^"]+)"/);
            if (match) {
                mp3Url = match[1].replace(/\\u0026/g, '&');
                break;
            }
        }

        if (mp3Url) {
            const urlPath = mp3Url.split('?')[0];
            const fileName = urlPath.substring(urlPath.lastIndexOf('/') + 1);
            const newBtn = document.createElement('a');
            newBtn.id = 'quick-mp3-download';
            newBtn.className = "MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary";
            newBtn.innerHTML = `<i class="fas fa-file-audio"></i> Download MP3`;
            newBtn.style.backgroundColor = "#4caf50"; // Green color
            newBtn.style.color = "white";
            newBtn.style.padding = "8px 16px";
            newBtn.style.borderRadius = "4px";
            newBtn.style.textDecoration = "none";
            newBtn.style.display = "flex";
            newBtn.style.alignItems = "center";
            newBtn.style.justifyContent = "center";
            newBtn.style.cursor = "pointer";
            newBtn.style.fontWeight = "bold";
            newBtn.style.fontSize = "16px";
            newBtn.style.flexDirection = "column";
            newBtn.title = "nothing here ig";

            newBtn.onclick = (e) => {
                e.preventDefault();
                GM_download({
                    url: mp3Url,
                    name: fileName,
                    saveAs: true
                });
            };

            originalBtnContainer.appendChild(newBtn);
        }
    }
    const observer = new MutationObserver(() => injectDownloadButton());
    observer.observe(document.body, { childList: true, subtree: true });
    injectDownloadButton();

})();