Easy SampleFocus℠ Downloader

Allows downloading from samplefocus.com without tokens.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Easy SampleFocus℠ Downloader
// @description Allows downloading from samplefocus.com without tokens.
// @description:ar Allows downloading from samplefocus.com without tokens.
// @description:bg Allows downloading from samplefocus.com without tokens.
// @description:cs Allows downloading from samplefocus.com without tokens.
// @description:da Allows downloading from samplefocus.com without tokens.
// @description:de Allows downloading from samplefocus.com without tokens.
// @description:el Allows downloading from samplefocus.com without tokens.
// @description:eo Allows downloading from samplefocus.com without tokens.
// @description:es Allows downloading from samplefocus.com without tokens.
// @description:fi Allows downloading from samplefocus.com without tokens.
// @description:fr Allows downloading from samplefocus.com without tokens.
// @description:fr-CA Allows downloading from samplefocus.com without tokens.
// @description:he Allows downloading from samplefocus.com without tokens.
// @description:hu Allows downloading from samplefocus.com without tokens.
// @description:id Allows downloading from samplefocus.com without tokens.
// @description:it Allows downloading from samplefocus.com without tokens.
// @description:ja Allows downloading from samplefocus.com without tokens.
// @description:ko Allows downloading from samplefocus.com without tokens.
// @description:nb Allows downloading from samplefocus.com without tokens.
// @description:nl Allows downloading from samplefocus.com without tokens.
// @description:pl Allows downloading from samplefocus.com without tokens.
// @description:ro Allows downloading from samplefocus.com without tokens.
// @description:ru Allows downloading from samplefocus.com without tokens.
// @description:sk Allows downloading from samplefocus.com without tokens.
// @description:sr Allows downloading from samplefocus.com without tokens.
// @description:sv Allows downloading from samplefocus.com without tokens.
// @description:th Allows downloading from samplefocus.com without tokens.
// @description:tr Allows downloading from samplefocus.com without tokens.
// @description:uk Allows downloading from samplefocus.com without tokens.
// @description:ug Allows downloading from samplefocus.com without tokens.
// @description:vi Allows downloading from samplefocus.com without tokens.
// @description:zh-CN Allows downloading from samplefocus.com without tokens.
// @description:zh-TW Allows downloading from samplefocus.com without tokens.
// @namespace   https://greasyfork.org/users/1627591
// @author Luftin
// @date 2026-07-25
// @version     1.0.0
// @match       https://samplefocus.com/samples/*
// @grant       none
// @license MIT
// ==/UserScript==

async function downloadFileFromUrl(downloadUrl, name) {
    try {
        const response = await fetch(downloadUrl);

        if (!response.ok) {
            throw new Error("Failed to download file");
        }

        const blob = await response.blob();
        const blobUrl = URL.createObjectURL(blob);

        const a = document.createElement("a");
        a.href = blobUrl;
        a.download = name;

        document.body.appendChild(a);
        a.click();
        a.remove();

        URL.revokeObjectURL(blobUrl);
    } catch (err) {
        console.error(err);
    }
}


function whitespaceToUnderscore(text) {
  return text.replace(/\s/g, "_");
}

async function loaded() {
  console.log("Loaded")

  const allE = document.querySelectorAll("script[type='application/ld+json']");

  allE.forEach((e) => {
    const string = e.innerText.toString();
    let obj = null;
    try{
      obj = JSON.parse(string);
    }catch(e){
      // console.debug("Parsing did not work on", string);
    }

    if(obj && obj.contentUrl){
      const downloadUrl = obj.contentUrl;

      let bpm = null;
      let key = null;
      let sample_name = whitespaceToUnderscore(document.querySelector(".MuiGrid-root.MuiGrid-item.MuiGrid-grid-xs-12.MuiGrid-grid-sm-6.MuiGrid-grid-md-6.MuiGrid-grid-lg-6").children[0].innerText);

      const attributes = document.querySelector("ul.sample-attrs");

      attributes.childNodes.forEach((node) => {
        const attribute = node.children[0].className;
        if (attribute.includes("fa-music")){
          key = whitespaceToUnderscore(node.innerText)
        }

        if (attribute.includes("fa-ellipsis")){
          bpm = whitespaceToUnderscore(node.innerText)
        }
      })

      const fileName = `${sample_name}${bpm ? "_" + bpm : ""}${key ? "_" + key : ""}`;

      async function handleDownloadClick(event) {
          downloadFileFromUrl(downloadUrl, fileName);
      }

      const originalDownloadButton = document.querySelector("*[aria-label='Download'].MuiButton-fullWidth");
      originalDownloadButton.setAttribute("style", "--variant-containedBg: #74be41");
      originalDownloadButton.innerText = "Download";
      console.log(originalDownloadButton);

      originalDownloadButton.addEventListener("click", (e) => {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        handleDownloadClick(e)
      }, true)
    }
  });
}


window.addEventListener('load', () => {
  setTimeout(loaded, 500);
});