Newgrounds Audio Download Fix

Adds a direct download button to audio pages on Newgrounds for songs that won't allow it.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Newgrounds Audio Download Fix
// @namespace    https://greasyfork.org/users/246635
// @version      1.1
// @description  Adds a direct download button to audio pages on Newgrounds for songs that won't allow it.
// @author       _darkuwu
// @match        https://www.newgrounds.com/audio/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener('load', main);

    function main() {
        // Check if the download button already exists
        if (document.querySelector('.icon-download')) return;

        // Get the pod-head div to place the new button
        const podHead = document.querySelector('.pod-head');
        if (!podHead) return;

        // Find the share button to place our new button next to it
        const shareButton = podHead.querySelector('.icon-share');
        if (!shareButton) return;


        const downloadButton = document.createElement('span');
        downloadButton.innerHTML = `
            <a class="icon-download" href="#" title="Download this song" rel="nofollow">
                Download this song
            </a>
        `;

        // Insert the download button next to the share button
        shareButton.insertAdjacentElement('afterend', downloadButton);

        // Find the audio URL from the meta tag
        const audioMeta = document.querySelector('meta[property="og:audio"]');
        if (!audioMeta) return;

        let audioUrl = audioMeta.getAttribute('content').split('?')[0]; // Trim to ".mp3"

        // Get the song ID from the filename
        const filename = audioUrl.split('/').pop().match(/^(\d+)_/)?.[1] || 'unknown';


        const anchor = downloadButton.querySelector('.icon-download');
        anchor.href = audioUrl;
        anchor.setAttribute('download', `${filename}.mp3`);


        anchor.addEventListener('click', function(event) {
            event.preventDefault();

            // Force download using a temporary <a> element
            const tempLink = document.createElement('a');
            tempLink.href = audioUrl;
            tempLink.download = `${filename}.mp3`;
            document.body.appendChild(tempLink);
            tempLink.click();
            document.body.removeChild(tempLink);
        });
    }
})();