Soundgasm Downloader

Adds a Download Audio button to Soundgasm.net audio posts.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         Soundgasm Downloader
// @namespace    https://github.com/diggtrap
// @author       diggtrap
// @description  Adds a Download Audio button to Soundgasm.net audio posts.
// @include      https://soundgasm.net/*
// @icon         https://i.imgur.com/BwX91V0.png
// @version      0.401
// @license      GNU
// ==/UserScript==

// Main function to add download button
function addDownloadButton() {
    // Fetch the page content asynchronously
    fetch(window.location.href)
        .then(response => response.text())
        .then(html => {
            // Parse the HTML content to extract the M4A link
            const m4aLinkRegex = /m4a:\s*"(.*?)"/;
            const m4aMatch = html.match(m4aLinkRegex);
            const m4aUrl = m4aMatch ? m4aMatch[1] : null;

            if (m4aUrl) {
                // Find the title of the audio post
                const audioTitleElement = document.querySelector('div.jp-title');
                const audioTitleText = audioTitleElement ? audioTitleElement.textContent.trim() : "";

                // Extract the text before the second set of square brackets
                const match = audioTitleText.match(/\[.*?\](.*?)(?=\[|$)/);
                const audioTitle = match ? match[0] : audioTitleText;

                // Find the creator's username from the URL
                const audioCreatorMatch = window.location.href.match(/\/u\/([^\/]+)/);
                const audioCreator = audioCreatorMatch ? audioCreatorMatch[1] : "";

                // Create download link with post title, creator, and default text style
                const downloadButton = document.createElement('a');
                downloadButton.href = m4aUrl;
                downloadButton.target = "_blank";
                downloadButton.title = "Click to open in a New Tab; Right-click and 'Save link as' to Download";
                downloadButton.textContent = `Download ${audioTitle} by ${audioCreator}`;
                downloadButton.style.position = "fixed";
                downloadButton.style.bottom = "20px";
                downloadButton.style.right = "20px";
                downloadButton.style.padding = "10px";
                downloadButton.style.backgroundColor = "#444";
                downloadButton.style.color = "white";
                downloadButton.style.border = "none";
                downloadButton.style.borderRadius = "5px";
                downloadButton.style.cursor = "pointer";
                downloadButton.style.textDecoration = "none";
                downloadButton.style.fontWeight = "normal";
                downloadButton.style.fontSize = "14px";

                document.body.appendChild(downloadButton);
            }
        });
}

// Call the main function
addDownloadButton();