Jinxin Util Download

文件下载工具类

当前为 2024-02-10 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/460643/1325652/Jinxin%20Util%20Download.js

// ==UserScript==
// @name         Jinxin Util Download
// @namespace    https://gitee.com/jinxin11112/tampermonkey
// @version      0.1.0
// @description  文件下载工具类
// @author       jinxin
// @icon         <$ICON$>
// @grant        none
// @license MIT
// ==/UserScript==

function downloadFile(fileContent, title) {
    const blob = new Blob(fileContent, {type: "text/plain;charset=utf-8"});
    // 创建新的URL并指向File对象或者Blob对象的地址
    const blobURL = window.URL.createObjectURL(blob)
    // 创建a标签,用于跳转至下载链接
    const tempLink = document.createElement('a')
    tempLink.style.display = 'none'
    tempLink.href = blobURL
    if (!title) title = Date.now();
    tempLink.download = decodeURI(title + '.txt')
    // 兼容:某些浏览器不支持HTML5的download属性
    if (typeof tempLink.download === 'undefined') {
        tempLink.setAttribute('target', '_blank')
    }
    // 挂载a标签
    document.body.appendChild(tempLink)
    tempLink.click()
    document.body.removeChild(tempLink)
    // 释放blob URL地址
    window.URL.revokeObjectURL(blobURL)
}

QingJ © 2025

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