Download Button for GreasyFork scripts

Adds a button to download the script

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

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

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

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

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

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

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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

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

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

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Download Button for GreasyFork scripts
// @version      1.2
// @author       Rust1667
// @description  Adds a button to download the script
// @match        https://greasyfork.org/*/scripts/*
// @match        https://sleazyfork.org/*/scripts/*
// @grant        none
// @icon         https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
// @namespace https://greasyfork.org/users/980489
// ==/UserScript==

(function() {
    'use strict';

    // Find the install link element
    var installLink = document.querySelector('#install-area .install-link');

    // Determine the file name
    const installURLlink = installLink.getAttribute('href');

    function getFilenameFromUrl() {
        const url = installURLlink;
        var lastSlashIndex = url.lastIndexOf('/');
        var filenameWithExtension = url.substring(lastSlashIndex + 1);
        var decodedFilename = filenameWithExtension.replace(/%20/g, '_');
        return decodedFilename;
    }

    // Create a download button
    var downloadButton = document.createElement('button');
    downloadButton.textContent = 'Download Script';
    downloadButton.style.marginLeft = '10px';

    // Add click event listener to the download button
    downloadButton.addEventListener('click', function() {
        var scriptUrl = installLink.getAttribute('href');
        downloadFile(scriptUrl);
    });

    // Insert the download button after the install link
    installLink.parentNode.insertBefore(downloadButton, installLink.nextSibling);

    function downloadFile(url) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = 'blob';
        xhr.onload = function() {
            if (xhr.status === 200) {
                var blob = xhr.response;
                var url = window.URL.createObjectURL(blob);
                var a = document.createElement('a');
                a.href = url;
                a.download = getFilenameFromUrl();
                document.body.appendChild(a);
                a.click();
                window.URL.revokeObjectURL(url);
            }
        };
        xhr.send();
    }
})();