Jinxin Util Download

文件下载工具类

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

  1. // ==UserScript==
  2. // @name Jinxin Util Download
  3. // @namespace https://gitee.com/jinxin11112/tampermonkey
  4. // @version 0.1.0
  5. // @description 文件下载工具类
  6. // @author jinxin
  7. // @icon https://picss.sunbangyan.cn/2024/02/11/a19cea286d366718e1358c1060c7c04e.jpeg
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. function downloadFile(fileContent, title) {
  13. const blob = new Blob(fileContent, {type: "text/plain;charset=utf-8"});
  14. // 创建新的URL并指向File对象或者Blob对象的地址
  15. const blobURL = window.URL.createObjectURL(blob)
  16. // 创建a标签,用于跳转至下载链接
  17. const tempLink = document.createElement('a')
  18. tempLink.style.display = 'none'
  19. tempLink.href = blobURL
  20. if (!title) title = Date.now();
  21. tempLink.download = decodeURI(title + '.txt')
  22. // 兼容:某些浏览器不支持HTML5的download属性
  23. if (typeof tempLink.download === 'undefined') {
  24. tempLink.setAttribute('target', '_blank')
  25. }
  26. // 挂载a标签
  27. document.body.appendChild(tempLink)
  28. tempLink.click()
  29. document.body.removeChild(tempLink)
  30. // 释放blob URL地址
  31. window.URL.revokeObjectURL(blobURL)
  32. }