FileDownloader-Module

Module providing file download functionality with GET and POST support

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/530648/1558616/FileDownloader-Module.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

作者
maanimis
版本
1.0
创建于
2025-03-23
更新于
2025-03-23
大小
2.8 KB
许可证
暂无

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