MCPcopy Create free account
hub / github.com/ChinaGodMan/UserScripts / downloadFile

Function downloadFile

github-raw-file-plus/github-raw-file-plus.user.js:194–212  ·  view source on GitHub ↗
(url, filename)

Source from the content-addressed store, hash-verified

192 return copyButton
193}
194function downloadFile(url, filename) {
195 var xhr = new XMLHttpRequest()
196 xhr.open('GET', url, true)
197 xhr.responseType = 'blob'
198 xhr.onload = function () {
199 if (xhr.status === 200) {
200 var blob = xhr.response
201 var objectUrl = window.URL.createObjectURL(blob)
202 var a = document.createElement('a')
203 a.href = objectUrl
204 a.download = filename // 设置下载文件名
205 document.body.appendChild(a)
206 a.click()
207 window.URL.revokeObjectURL(objectUrl) // 清理 object URL
208 document.body.removeChild(a) // 清理 DOM
209 }
210 }
211 xhr.send()
212}
213function getFilenameFromUrl(url) {
214 if (typeof url !== 'string' || url.trim() === '') {
215 logMessage('getFilenameFromUrl', 'URL无效,默认文件名download', false)

Callers 1

creatyDownButtonFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected