Skip to content

Commit 2083d6b

Browse files
authored
解决Firefox+Violentmonkey 使用脚本不能打包下载的问题
1.arrayBuffer() 比 blob() 更可靠:在跨域和 CSP 受限环境下,ArrayBuffer 的获取策略更严格但更稳定,JSZip 可以完全读取二进制数据。 2.Uint8Array 是 JSZip 原生支持的类型:避免浏览器对 blob 的“不透明”包装。
1 parent fce546f commit 2083d6b

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

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

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -613,40 +613,56 @@ const TMD = (function () {
613613
tasks.push(...taskList)
614614
this.update()
615615
if (enable_packaging) {
616-
let zip = new JSZip()
617-
let completedCount = 0
616+
let zip = new JSZip();
617+
let completedCount = 0;
618618
taskList.forEach((task, i) => {
619-
thread++
620-
this.update()
619+
thread++;
620+
this.update();
621621
fetch(task.url)
622-
.then(response => response.blob())
623-
.then(blob => {
624-
zip.file(task.name, blob)
625-
tasks = tasks.filter(t => t.url !== task.url)
626-
thread--
627-
this.update()
628-
completedCount++
622+
.then(response => {
623+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
624+
return response.arrayBuffer(); // ← 关键修改
625+
})
626+
.then(buffer => {
627+
const uint8Array = new Uint8Array(buffer);
628+
zip.file(task.name, uint8Array);
629+
tasks = tasks.filter(t => t.url !== task.url);
630+
thread--;
631+
this.update();
632+
completedCount++;
629633
if (completedCount === taskList.length) {
630634
zip.generateAsync({ type: 'blob' }).then(content => {
631-
let a = document.createElement('a')
632-
a.href = URL.createObjectURL(content)
633-
a.download = `${taskList[0].name}.zip`
634-
a.click()
635-
this.status(btn, 'completed', lang.completed)
636-
if (save_history && !is_exist) {
637-
history.push(status_id)
638-
this.storage(status_id)
639-
}
640-
})
635+
// 使用 GM_download 下载 ZIP(避免 Firefox 的 a.click 拦截)
636+
const zipBlob = new Blob([content], { type: 'application/zip' });
637+
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);
647+
}
648+
},
649+
onerror: (err) => {
650+
URL.revokeObjectURL(zipUrl);
651+
this.status(btn, 'failed', err.details?.current || 'ZIP download failed');
652+
}
653+
});
654+
}).catch(err => {
655+
this.status(btn, 'failed', err.message);
656+
});
641657
}
642658
})
643659
.catch(error => {
644-
failed++
645-
tasks = tasks.filter(t => t.url !== task.url)
646-
this.status(btn, 'failed', error.message)
647-
this.update()
648-
})
649-
})
660+
failed++;
661+
tasks = tasks.filter(t => t.url !== task.url);
662+
this.status(btn, 'failed', error.message);
663+
this.update();
664+
});
665+
});
650666
} else {
651667
taskList.forEach((task) => {
652668
thread++

0 commit comments

Comments
 (0)