Greasy Fork镜像 支持简体中文。

FileDownloader-Module

Module providing file download functionality with GET and POST support

目前為 2025-03-23 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/530648/1558616/FileDownloader-Module.js

  1. // ==UserScript==
  2. // @name FileDownloader-Module
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Module providing file download functionality with GET and POST support
  6. // @author maanimis
  7. // @grant GM_xmlhttpRequest
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. class FileDownloader {
  15. static async fetchAsBlob(url, options = {}) {
  16. const defaultOptions = {
  17. method: 'GET',
  18. headers: {},
  19. body: null,
  20. };
  21.  
  22. const fetchOptions = { ...defaultOptions, ...options };
  23.  
  24. return new Promise((resolve, reject) => {
  25. GM_xmlhttpRequest({
  26. method: fetchOptions.method,
  27. url: url,
  28. headers: fetchOptions.headers,
  29. data: fetchOptions.body,
  30. responseType: 'blob',
  31. onload: function (response) {
  32. if (response.status >= 200 && response.status < 300) {
  33. resolve(response.response);
  34. } else {
  35. reject(new Error(`HTTP error! Status: ${response.status}`));
  36. }
  37. },
  38. onerror: function (error) {
  39. reject(new Error('Network error occurred'));
  40. },
  41. });
  42. });
  43. }
  44.  
  45. static async fetchWithPost(url, data, headers = {}) {
  46. const options = {
  47. method: 'POST',
  48. headers: {
  49. 'Content-Type': 'application/json',
  50. ...headers,
  51. },
  52. body: JSON.stringify(data),
  53. };
  54. return this.fetchAsBlob(url, options);
  55. }
  56.  
  57. static async fetchWithFormData(url, formData, headers = {}) {
  58. // When using FormData with GM_xmlhttpRequest, we don't need to set Content-Type
  59. // as it will be set automatically with the correct boundary
  60. const options = {
  61. method: 'POST',
  62. headers: {
  63. ...headers,
  64. },
  65. body: formData,
  66. };
  67. return this.fetchAsBlob(url, options);
  68. }
  69.  
  70. static createObjectUrl(blob) {
  71. return URL.createObjectURL(blob);
  72. }
  73.  
  74. static revokeObjectUrl(url) {
  75. URL.revokeObjectURL(url);
  76. }
  77.  
  78. static triggerDownload(blob, filename) {
  79. const url = this.createObjectUrl(blob);
  80. const link = document.createElement('a');
  81. link.href = url;
  82. link.download = filename;
  83. link.style.display = 'none';
  84. document.body.appendChild(link);
  85. link.click();
  86. document.body.removeChild(link);
  87. this.revokeObjectUrl(url);
  88. }
  89.  
  90. static openInNewTab(blob) {
  91. const url = this.createObjectUrl(blob);
  92. window.open(url);
  93. return url;
  94. }
  95. }
  96.  
  97. // Add to window as a module
  98. window.FileDownloaderModule = FileDownloader;
  99.  
  100. // Dispatch an event to notify other scripts that the module is loaded
  101. const event = new CustomEvent('FileDownloaderModuleLoaded');
  102. document.dispatchEvent(event);
  103. console.log('FileDownloader module loaded');
  104. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址