FileDownloader-Module

Module providing file download functionality with GET and POST support

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js

Autor
maanimis
Version
1.0
Erstellt am
23.03.2025
Letzte Aktualisierung
23.03.2025
Größe
2,81 KB
Lizenz
n/a

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