FileDownloader-Module

Module providing file download functionality with GET and POST support

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

Autor
maanimis
Verze
1.0
Vytvořeno
23. 03. 2025
Aktualizováno
23. 03. 2025
Size
2,8 KB
Licence
neuvedeno

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;
        }
    }
})();