Files
mangabaka-seeder/src/web/save-file.ts
T
2026-10-15 11:32:51 +02:00

11 lines
563 B
TypeScript

/** Hands bytes to the browser as a download. Works from the page and from the userscript's shadow root. */
export function saveFile(bytes: Uint8Array, name: string, type: string, beside: Element): void {
const url = URL.createObjectURL(new Blob([bytes as Uint8Array<ArrayBuffer>], { type }));
const a = Object.assign(document.createElement("a"), { href: url, download: name, hidden: true });
beside.after(a);
a.click();
a.remove();
// Revoking at once can cancel the download in some browsers.
setTimeout(() => URL.revokeObjectURL(url), 30_000);
}