@@ -128,15 +128,15 @@ window.addEventListener("dragover", e => e.preventDefault());
128128 * Display an on-screen popup.
129129 * @param html HTML content of the popup box.
130130 */
131- function showPopup ( html : string ) {
131+ window . showPopup = function ( html : string ) {
132132 ui . popupBox . innerHTML = html ;
133133 ui . popupBox . style . display = "block" ;
134134 ui . popupBackground . style . display = "block" ;
135135}
136136/**
137137 * Hide the on-screen popup.
138138 */
139- function hidePopup ( ) {
139+ window . hidePopup = function ( ) {
140140 ui . popupBox . style . display = "none" ;
141141 ui . popupBackground . style . display = "none" ;
142142}
@@ -234,7 +234,7 @@ async function buildOptionList () {
234234 filterButtonList ( ui . inputList , ui . inputSearch . value ) ;
235235 filterButtonList ( ui . outputList , ui . outputSearch . value ) ;
236236
237- hidePopup ( ) ;
237+ window . hidePopup ( ) ;
238238
239239}
240240
@@ -273,7 +273,7 @@ const convertPathCache: Array<{
273273async function attemptConvertPath ( files : FileData [ ] , path : ConvertPathNode [ ] ) {
274274
275275 ui . popupBox . innerHTML = `<h2>Finding conversion route...</h2>
276- <p>Trying ${ path . map ( c => c . format . format ) . join ( " -> " ) } </p>` ;
276+ <p>Trying <b> ${ path . map ( c => c . format . format ) . join ( " → " ) } </b>... </p>` ;
277277
278278 const cacheLast = convertPathCache . at ( - 1 ) ;
279279 if ( cacheLast ) files = cacheLast . files ;
@@ -297,7 +297,7 @@ async function attemptConvertPath (files: FileData[], path: ConvertPathNode[]) {
297297 convertPathCache . push ( { files, node : path [ i + 1 ] } ) ;
298298 } catch ( e ) {
299299 console . log ( path . map ( c => c . format . format ) ) ;
300- console . error ( handler . name , `${ path [ i ] . format . format } -> ${ path [ i + 1 ] . format . format } ` , e ) ;
300+ console . error ( handler . name , `${ path [ i ] . format . format } → ${ path [ i + 1 ] . format . format } ` , e ) ;
301301 return null ;
302302 }
303303 }
@@ -371,6 +371,14 @@ async function buildConvertPath (
371371
372372}
373373
374+ function downloadFile ( bytes : Uint8Array , name : string , mime : string ) {
375+ const blob = new Blob ( [ bytes as BlobPart ] , { type : mime } ) ;
376+ const link = document . createElement ( "a" ) ;
377+ link . href = URL . createObjectURL ( blob ) ;
378+ link . download = name ;
379+ link . click ( ) ;
380+ }
381+
374382ui . convertButton . onclick = async function ( ) {
375383
376384 const inputFiles = selectedFiles ;
@@ -388,46 +396,47 @@ ui.convertButton.onclick = async function () {
388396 const inputOption = allOptions [ Number ( inputButton . getAttribute ( "format-index" ) ) ] ;
389397 const outputOption = allOptions [ Number ( outputButton . getAttribute ( "format-index" ) ) ] ;
390398
399+ const inputFormat = inputOption . format ;
400+ const outputFormat = outputOption . format ;
401+
391402 try {
392403
393404 const inputFileData = [ ] ;
394405 for ( const inputFile of inputFiles ) {
395406 const inputBuffer = await inputFile . arrayBuffer ( ) ;
396407 const inputBytes = new Uint8Array ( inputBuffer ) ;
408+ if ( inputFormat . mime === outputFormat . mime ) {
409+ downloadFile ( inputBytes , inputFile . name , inputFormat . mime ) ;
410+ continue ;
411+ }
397412 inputFileData . push ( { name : inputFile . name , bytes : inputBytes } ) ;
398413 }
399414
400- showPopup ( "<h2>Finding conversion route...</h2>" ) ;
415+ window . showPopup ( "<h2>Finding conversion route...</h2>" ) ;
401416
402417 const output = await buildConvertPath ( inputFileData , outputOption , [ [ inputOption ] ] ) ;
403418 if ( ! output ) {
404- hidePopup ( ) ;
419+ window . hidePopup ( ) ;
405420 alert ( "Failed to find conversion route." ) ;
406421 return ;
407422 }
408423
409- const outputFormat = outputOption . format ;
410-
411424 for ( const file of output . files ) {
412- const blob = new Blob ( [ file . bytes as BlobPart ] , { type : outputFormat . mime } ) ;
413- const link = document . createElement ( "a" ) ;
414- link . href = URL . createObjectURL ( blob ) ;
415- link . download = file . name ;
416- link . click ( ) ;
425+ downloadFile ( file . bytes , file . name , outputFormat . mime ) ;
417426 }
418427
419- alert (
420- `Converted ${ inputOption . format . format } to ${ outputOption . format . format } !\n` +
421- `Path used: ${ output . path . map ( c => c . format . format ) . join ( " -> " ) } `
428+ window . showPopup (
429+ `<h2>Converted ${ inputOption . format . format } to ${ outputOption . format . format } !</h2>` +
430+ `<p>Path used: <b>${ output . path . map ( c => c . format . format ) . join ( " → " ) } </b>.</p>\n` +
431+ `<button onclick="window.hidePopup()">OK</button>`
422432 ) ;
423433
424434 } catch ( e ) {
425435
436+ window . hidePopup ( ) ;
426437 alert ( "Unexpected error while routing:\n" + e ) ;
427438 console . error ( e ) ;
428439
429440 }
430441
431- hidePopup ( ) ;
432-
433442} ;
0 commit comments