import { DonwloadListData , SendToBackEndData } from './modules/Download.d' ; chrome . webRequest . onBeforeSendHeaders . addListener ( function ( details ) { if ( details . url . includes ( 'api.fanbox.cc/' ) ) { let hasOrigin = false ; for ( const HttpHeader of details . requestHeaders ! ) { if ( HttpHeader . name === 'Origin' ) { hasOrigin = true ; break ; } } if ( ! hasOrigin ) { details . requestHeaders ! . push ( { name : 'Origin' , value : details . initiator , } ) ; details . requestHeaders ! . push ( { name : 'Referer' , value : details . initiator + '/' , } ) ; } } return { requestHeaders : details . requestHeaders , } ; } , { urls : [ '*://*.fanbox.cc/*' ] , } , [ 'blocking' , 'requestHeaders' , 'extraHeaders' ] ) ; chrome . browserAction . onClicked . addListener ( function ( tab ) { chrome . tabs . sendMessage ( tab . id ! , { msg : 'click_icon' , } ) ; } ) ; let dlData = { } ; let dlBatch = [ ] ; chrome . runtime . onMessage . addListener ( function ( msg , sender ) { if ( msg . msg === 'send_download' ) { const tabId = sender . tab ! . id ! ; if ( dlBatch [ tabId ] !== msg . taskBatch ) { dlBatch [ tabId ] = msg . taskBatch ; } chrome . downloads . download ( { url : msg . fileUrl , filename : msg . fileName , conflictAction : 'uniquify' , saveAs : false , } , ( id ) => { dlData [ id ] = { url : msg . fileUrl , id : msg . id , tabId : tabId , uuid : false , } ; } ) ; } } ) ; const UUIDRegexp = /[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}/ ; chrome . downloads . onChanged . addListener ( function ( detail ) { const data = dlData [ detail . id ] ; if ( data ) { let msg = '' ; let err = '' ; if ( detail . filename && detail . filename . current ) { const changedName = detail . filename . current ; if ( changedName . endsWith ( 'jfif' ) || changedName . match ( UUIDRegexp ) !== null ) { data . uuid = true ; } } if ( detail . state && detail . state . current === 'complete' ) { msg = 'downloaded' ; } if ( detail . error && detail . error . current ) { msg = 'download_err' ; err = detail . error . current ; } if ( msg ) { chrome . tabs . sendMessage ( data . tabId , { msg , data , err } ) ; dlData [ detail . id ] = null ; } } } ) ;