Easy SampleFocus℠ Downloader

Allows downloading from samplefocus.com without tokens.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);
});