Skip to content

Commit cdaefc3

Browse files
authored
改进Firefox和Edge/Chrome等zip下载的实现方式
不同浏览器下,Violentmonkey 对 GM_download + Blob URL 的支持存在差异,浏览器自适应下载。
1 parent ce02dca commit cdaefc3

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

twitter-media-downloader/twitter-media-downloader.user.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -632,28 +632,52 @@ const TMD = (function () {
632632
completedCount++
633633
if (completedCount === taskList.length) {
634634
zip.generateAsync({ type: 'blob' }).then(content => {
635-
// 使用 GM_download 下载 ZIP(避免 Firefox 的 a.click 拦截)
636635
const zipBlob = new Blob([content], { type: 'application/zip' })
637636
const zipUrl = URL.createObjectURL(zipBlob)
638-
GM_download({
639-
url: zipUrl,
640-
name: `${taskList[0].name}.zip`,
641-
onload: () => {
642-
URL.revokeObjectURL(zipUrl)
643-
this.status(btn, 'completed', lang.completed)
644-
if (save_history && !is_exist) {
645-
history.push(status_id)
646-
this.storage(status_id)
637+
const zipFileName = `${taskList[0].name}.zip`
638+
639+
// 检测是否为 Firefox
640+
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
641+
642+
// Firefox 使用 GM_download
643+
if (isFirefox) {
644+
GM_download({
645+
url: zipUrl,
646+
name: zipFileName,
647+
onload: () => {
648+
URL.revokeObjectURL(zipUrl)
649+
this.status(btn, 'completed', lang.completed)
650+
if (save_history && !is_exist) {
651+
history.push(status_id)
652+
this.storage(status_id)
653+
}
654+
},
655+
onerror: (err) => {
656+
URL.revokeObjectURL(zipUrl)
657+
this.status(btn, 'failed', err.details?.current || 'ZIP download failed')
647658
}
648-
},
649-
onerror: (err) => {
659+
})
660+
} else {
661+
// Chrome / Edge / Opera 等使用传统 a.click 方式
662+
const a = document.createElement('a')
663+
a.href = zipUrl
664+
a.download = zipFileName
665+
document.body.appendChild(a)
666+
a.click()
667+
// 延迟移除,确保下载开始
668+
setTimeout(() => {
669+
document.body.removeChild(a)
650670
URL.revokeObjectURL(zipUrl)
651-
this.status(btn, 'failed', err.details?.current || 'ZIP download failed');
671+
}, 100)
672+
this.status(btn, 'completed', lang.completed)
673+
if (save_history && !is_exist) {
674+
history.push(status_id)
675+
this.storage(status_id)
652676
}
653-
})
677+
}
654678
}).catch(err => {
655679
this.status(btn, 'failed', err.message)
656-
});
680+
})
657681
}
658682
})
659683
.catch(error => {

0 commit comments

Comments
 (0)