FileDownloader-Module

Module providing file download functionality with GET and POST support

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

Autore
maanimis
Versione
1.0
Creato il
23/03/2025
Aggiornato il
23/03/2025
Dimensione
2,81 KB
Licenza
Non disponibile

sample

// ==UserScript==
// @name        Sample File Downloader
// @namespace   http://tampermonkey.net/
// @version     1.0
// @description Demonstrates using the FileDownloader Module to download files
// @author      You
// @match       *://*/*
// @grant       GM_xmlhttpRequest
// @require     https://update.greasyfork.org/scripts/530648/1558612/FileDownloader-Module.js
// ==/UserScript==

(function() {
    'use strict';

    // Wait for the document to fully load
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initScript);
    } else {
        initScript();
    }

    // Wait for the FileDownloader module to be loaded
    document.addEventListener('FileDownloaderModuleLoaded', () => {
        console.log('FileDownloader module detected and ready to use!');
    });

    function initScript() {
        // Create a download button and add it to the page
        const downloadButton = document.createElement('button');
        downloadButton.textContent = 'Download Sample File';
        downloadButton.style.position = 'fixed';
        downloadButton.style.top = '10px';
        downloadButton.style.right = '10px';
        downloadButton.style.zIndex = '9999';
        downloadButton.style.padding = '10px';
        downloadButton.style.backgroundColor = '#4CAF50';
        downloadButton.style.color = 'white';
        downloadButton.style.border = 'none';
        downloadButton.style.borderRadius = '5px';
        downloadButton.style.cursor = 'pointer';

        // Add click event to download a file
        downloadButton.addEventListener('click', async () => {
            try {
                // Example 1: Simple GET request to download a text file
                await downloadTextFile();

                // Example 2: Download a JSON file using POST
                // Uncomment to test this example
                // await downloadWithPost();

                // Example 3: Download using FormData
                // Uncomment to test this example
                // await downloadWithFormData();

            } catch (error) {
                console.error('Download failed:', error);
                alert('Download failed: ' + error.message);
            }
        });

        document.body.appendChild(downloadButton);
    }

    // Example 1: Download a text file using GET
    async function downloadTextFile() {
        // Sample URL - replace with your actual source
        const url = 'https://jsonplaceholder.typicode.com/todos/1';

        try {
            // Use the FileDownloader module to fetch the file
            const blob = await window.FileDownloaderModule.fetchAsBlob(url);

            // Trigger the download with a custom filename
            window.FileDownloaderModule.triggerDownload(blob, 'sample-data.json');

            console.log('Download completed successfully');
        } catch (error) {
            console.error('Error downloading file:', error);
            throw error;
        }
    }

    // Example 2: Download using POST request with JSON data
    async function downloadWithPost() {
        const url = 'https://jsonplaceholder.typicode.com/posts';
        const data = {
            title: 'Sample Post Request',
            body: 'This is a test post request',
            userId: 1
        };

        try {
            const blob = await window.FileDownloaderModule.fetchWithPost(url, data);
            window.FileDownloaderModule.triggerDownload(blob, 'post-response.json');
            console.log('POST download completed successfully');
        } catch (error) {
            console.error('Error with POST download:', error);
            throw error;
        }
    }

    // Example 3: Download using FormData
    async function downloadWithFormData() {
        const url = 'https://jsonplaceholder.typicode.com/posts';
        const formData = new FormData();
        formData.append('title', 'Sample Form Data Request');
        formData.append('body', 'This is a test using FormData');
        formData.append('userId', '1');

        try {
            const blob = await window.FileDownloaderModule.fetchWithFormData(url, formData);
            window.FileDownloaderModule.triggerDownload(blob, 'formdata-response.json');
            console.log('FormData download completed successfully');
        } catch (error) {
            console.error('Error with FormData download:', error);
            throw error;
        }
    }
})();