@@ -67,12 +67,18 @@ function resetOutput() {
6767 footer . style . top = '-3rem' ;
6868}
6969
70- async function showImage ( imBlob , format ) {
70+ /**
71+ * Prepare the output area: display the image, and set up the download button.
72+ * @param {string } type - 'encoded' / 'decoded'
73+ * @param {Uint8Array } imBlob - The image data as a Uint8Array
74+ * @param {string } format - The image format, 'png'/'jpeg'
75+ */
76+ async function showImage ( type , imBlob , format ) {
7177 resetOutput ( ) ;
7278 ensureOutput ( ) ;
7379
7480 hintLabel . textContent = '' ;
75- const blob = new Blob ( [ imBlob ] , { type : ' image/png' } ) ;
81+ const blob = new Blob ( [ imBlob ] , { type : ` image/${ format } ` } ) ;
7682 const url = URL . createObjectURL ( blob ) ;
7783
7884 const imgElem = document . createElement ( 'img' ) ;
@@ -84,8 +90,8 @@ async function showImage(imBlob, format) {
8490 downloadBtn . onclick = ( ) => {
8591 const a = document . createElement ( 'a' ) ;
8692 a . href = url ;
87- const timeName = new Date ( ) . toISOString ( ) . replace ( / [: . ] / g , '-' ) ;
88- a . download = `img -${ timeName } .${ format } ` ;
93+ const uuid = crypto . randomUUID ( ) . slice ( 0 , 8 ) ;
94+ a . download = `${ type } -${ uuid } .${ format } ` ;
8995 document . body . appendChild ( a ) ;
9096 a . click ( ) ;
9197 document . body . removeChild ( a ) ;
@@ -130,9 +136,11 @@ worker.onmessage = async (event) => {
130136 if ( event . data . error ) {
131137 await showError ( `Error: ${ event . data . error } ` ) ;
132138 console . error ( event . data . error ) ;
139+ hintLabel . textContent = '' ;
133140 return ;
134141 }
135142 await showImage (
143+ event . data . type ,
136144 event . data . buffer ,
137145 event . data . format
138146 ) ;
@@ -154,10 +162,10 @@ worker.onmessage = async (event) => {
154162
155163 fileInput . addEventListener ( 'change' , ( ) => {
156164 resetOutput ( ) ;
157- hintLabel . textContent = '(The selected image)' ;
158165 const file = fileInput . files [ 0 ] ;
159166 if ( file ) {
160167 ensureOutput ( ) ;
168+ hintLabel . textContent = '(The selected image)' ;
161169 const imgElem = document . createElement ( 'img' ) ;
162170 imgElem . src = URL . createObjectURL ( file ) ;
163171 imgElem . alt = 'Selected Image' ;
0 commit comments