SampleFocus Downloader [fixed lol]

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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();

})();