FileDownloader-Module

Module providing file download functionality with GET and POST support

Tätä skriptiä ei tulisi asentaa suoraan. Se on kirjasto muita skriptejä varten sisällytettäväksi metadirektiivillä // @require https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

Tekijä
maanimis
Versio
1.0
Luotu
23.3.2025
Päivitetty
23.3.2025
Size
2,81 kt
Lisenssi
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;
        }
    }
})();