GM_DL

GM_download but it actually works

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/523457/1519388/GM_DL.js

  1. // ==UserScript==
  2. // @name GM_DL
  3. // @namespace Violentmonkey Scripts
  4. // @grant GM.xmlHttpRequest
  5. // @version 1.3
  6. // @author https://greasyfork.org/en/users/1409235-paywalldespiser
  7. // @description GM_download but it actually works
  8. // @license MIT
  9. // @require https://cdn.jsdelivr.net/npm/file-saver@2.0.5/dist/FileSaver.min.js
  10. // ==/UserScript==
  11.  
  12. /**
  13. * Arguments for GM_DL
  14. *
  15. * @typedef {object} GM_DL_ARGS
  16. * @property {string | URL} url
  17. * @property {string} filename
  18. * @property {("GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "TRACE" | "OPTIONS" | "CONNECT")} [method='GET']
  19. * @property {("follow" | "error" | "manual")} [redirect='follow']
  20. * @property {string | undefined} data
  21. * @property {{ [key: string]: string; } | undefined} headers
  22. */
  23. /**
  24. * GM_download but it actually works
  25. *
  26. * @param {GM_DL_ARGS}
  27. * @returns {Promise<void>}
  28. */
  29. function GM_DL({
  30. url,
  31. filename,
  32. method = 'GET',
  33. redirect = 'follow',
  34. data,
  35. headers,
  36. }) {
  37. return GM.xmlHttpRequest({
  38. url,
  39. method,
  40. responseType: 'blob',
  41. redirect,
  42. data,
  43. headers,
  44. }).then(({ response }) => saveAs(response, filename));
  45. }