import { EVT } from './EVT' ; import { titleBar } from './TitleBar' ; import { fileName } from './FileName' ; import { downloadArgument , SendToBackEndData , DonwloadSuccessData , } from './Download.d' ; import { progressBar } from './ProgressBar' ; class Download { constructor ( progressBarIndex , data ) { this . progressBarIndex = progressBarIndex ; this . arg = data ; this . download ( data ) ; this . listenEvents ( ) ; } private progressBarIndex : number ; private arg : downloadArgument ; private fileName = '' ; private stoped = false ; private listenEvents ( ) { ; [ EVT . events . downloadStop , EVT . events . downloadPause ] . forEach ( ( event ) => { window . addEventListener ( event , ( ) => { this . stoped = true ; } ) ; } ) ; window . addEventListener ( EVT . events . downloadSucccess , ( event ) => { const donwloadSuccessData = event . detail . data as DonwloadSuccessData ; if ( donwloadSuccessData . url === this . arg . data . url ) { this . setProgressBar ( 1024 , 1024 ) ; } } ) ; } private setProgressBar ( loaded , total ) { progressBar . setProgress ( this . progressBarIndex , { name : this . fileName , loaded : loaded , total : total , } ) ; } private download ( arg ) { titleBar . change ( '↓' ) ; this . fileName = fileName . getFileName ( arg . data ) ; this . setProgressBar ( 0 , 0 ) ; this . browserDownload ( arg . data . url , this . fileName , arg . id , arg . taskBatch ) ; } private browserDownload ( url , fileName , id , taskBatch ) { const sendData = { msg : 'send_download' , fileUrl : url , fileName : fileName , id , taskBatch , } ; chrome . runtime . sendMessage ( sendData ) ; } } export { Download } ;