MCPcopy Index your code
hub / github.com/hoothin/UserScripts / downloadByFetch

Function downloadByFetch

X-Downloader/X-Downloader.user.js:98–115  ·  view source on GitHub ↗
(imageUrl, filename)

Source from the content-addressed store, hash-verified

96 }, 100);
97 });
98 async function downloadByFetch(imageUrl, filename) {
99 try {
100 const response = await fetch(imageUrl);
101 if (!response.ok) throw new Error('CORS request failed');
102 const blob = await response.blob();
103 const blobUrl = URL.createObjectURL(blob);
104 const tempLink = document.createElement('a');
105 tempLink.href = blobUrl;
106 tempLink.setAttribute('download', filename);
107 document.body.appendChild(tempLink);
108 tempLink.click();
109 document.body.removeChild(tempLink);
110 URL.revokeObjectURL(blobUrl);
111 } catch (error) {
112 console.error('error:', error);
113 window.open(imageUrl, '_blank');
114 }
115 }
116 const show = (ele) => {
117 ele.appendChild(downloadBtn);
118 setTimeout(() => {

Callers 1

Calls 1

fetchFunction · 0.85

Tested by

no test coverage detected