FileDownloader-Module

Module providing file download functionality with GET and POST support

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name FileDownloader-Module
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Module providing file download functionality with GET and POST support
// @author maanimis
// @grant GM_xmlhttpRequest
// @run-at document-start
// ==/UserScript==

(function () {
  'use strict';

  class FileDownloader {
    static async fetchAsBlob(url, options = {}) {
      const defaultOptions = {
        method: 'GET',
        headers: {},
        body: null,
      };

      const fetchOptions = { ...defaultOptions, ...options };

      return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
          method: fetchOptions.method,
          url: url,
          headers: fetchOptions.headers,
          data: fetchOptions.body,
          responseType: 'blob',
          onload: function (response) {
            if (response.status >= 200 && response.status < 300) {
              resolve(response.response);
            } else {
              reject(new Error(`HTTP error! Status: ${response.status}`));
            }
          },
          onerror: function (error) {
            reject(new Error('Network error occurred'));
          },
        });
      });
    }

    static async fetchWithPost(url, data, headers = {}) {
      const options = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          ...headers,
        },
        body: JSON.stringify(data),
      };
      return this.fetchAsBlob(url, options);
    }

    static async fetchWithFormData(url, formData, headers = {}) {
      // When using FormData with GM_xmlhttpRequest, we don't need to set Content-Type
      // as it will be set automatically with the correct boundary
      const options = {
        method: 'POST',
        headers: {
          ...headers,
        },
        body: formData,
      };
      return this.fetchAsBlob(url, options);
    }

    static createObjectUrl(blob) {
      return URL.createObjectURL(blob);
    }

    static revokeObjectUrl(url) {
      URL.revokeObjectURL(url);
    }

    static triggerDownload(blob, filename) {
      const url = this.createObjectUrl(blob);
      const link = document.createElement('a');
      link.href = url;
      link.download = filename;
      link.style.display = 'none';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      this.revokeObjectUrl(url);
    }

    static openInNewTab(blob) {
      const url = this.createObjectUrl(blob);
      window.open(url);
      return url;
    }
  }

  // Add to window as a module
  window.FileDownloaderModule = FileDownloader;

  // Dispatch an event to notify other scripts that the module is loaded
  const event = new CustomEvent('FileDownloaderModuleLoaded');
  document.dispatchEvent(event);
  console.log('FileDownloader module loaded');
})();