class NZBUnityTabulaRasa { public apikey : string ; public replace : boolean = false ; constructor ( ) { this . initialize ( ) ; } async initialize ( ) { const { Providers , ReplaceLinks } = await Util . storage . get ( [ 'Providers' , 'ReplaceLinks' ] ) ; this . replace = ReplaceLinks ; const provider = Providers && Providers . tabularasa ; const enabled = provider ? provider . Enabled : true ; if ( enabled ) { console . info ( `[NZB Unity] Initializing 1-click functionality...` ) ; this . apikey = await this . getApiKey ( ) ; if ( this . apikey ) { console . info ( `Got api key: ${ this . apikey }` ) ; return this . initializeLinks ( ) ; } else { console . warn ( 'Could not get API key' ) ; } } else { console . info ( `[NZB Unity] 1-click functionality disabled for this site` ) ; } } async getApiKey ( ) { const r = await PageUtil . request ( { url : '/profile' } ) ; const [ match , token ] = r . match ( /api_token=([a-f0-9]+)/i ) ; return token ; } getNzbUrl ( id ) { return `${ window . location . origin }/api/v2/getnzb?id=${ id }&api_token=${ this . apikey }` ; } initializeLinks ( ) { $ ( 'a[href*="/getnzb?id="]' ) . each ( ( i , el ) => { const a = $ ( el ) ; const [ match , id ] = a . attr ( 'href' ) . match ( /\/getnzb\?id=([\w-]+)/i ) ; const url = this . getNzbUrl ( id ) ; let category = '' ; let catSrc = 'default' ; if ( window . location . pathname . startsWith ( '/details' ) ) { const catLink = $ ( '.tab-content a[href*="/browse/"]' ) . filter ( ':not([href*="?"])' ) ; if ( catLink . length ) { category = catLink . text ( ) ; catSrc = 'href' ; } category = category . split ( /[^\w-]/ ) [ 0 ] ; const linkOpts = { url , category } ; if ( this . replace ) { PageUtil . bindAddUrl ( linkOpts , a , true ) ; } else { const link = PageUtil . createAddUrlLink ( linkOpts ) ; link . html ( `${ link . html ( ) } NZB Unity` ) . addClass ( 'btn btn-light btn-sm btn-success btn-transparent' ) . css ( { display : '' , height : '' , width : '' , 'margin-left' : '-1px' } ) . insertBefore ( a ) ; } } else if ( a . attr ( 'role' ) === 'button' ) { const linkOpts = { url , category : null } ; if ( this . replace ) { PageUtil . bindAddUrl ( linkOpts , a , true ) ; } else { const link = PageUtil . createAddUrlLink ( linkOpts ) . css ( { float : 'left' , margin : '0 0.25em 0 0' } ) ; a . parent ( ) . parent ( ) . find ( '.release-name' ) . prepend ( link ) ; } } else { category = a . closest ( 'tr[id^="guid"]' ) . find ( 'td:nth-child(3)' ) . text ( ) ; catSrc = 'href' ; category = category . split ( /[^\w-]/ ) [ 0 ] ; const linkOpts = { url , category } ; if ( this . replace ) { PageUtil . bindAddUrl ( linkOpts , a , true ) ; } else { const link = PageUtil . createAddUrlLink ( linkOpts ) . css ( { margin : '0 0.25em 0 0' } ) . insertBefore ( a ) ; } } } ) ; $ ( '.nzb_multi_operations_download' ) . each ( ( i , el ) => { const getNzbUrl = ( id ) => this . getNzbUrl ( id ) ; let button = PageUtil . createButton ( ) . text ( 'NZB Unity' ) . addClass ( 'btn btn-sm' ) . css ( { 'border-top-right-radius' : '0' , 'border-bottom-right-radius' : '0' , 'line-heignt' : '11px' , 'margin' : '0' , } ) . on ( 'click' , ( e ) => { e . preventDefault ( ) ; const checked = $ ( 'input[name="table_records"]:checked' ) ; if ( checked . length ) { console . info ( `[NZB Unity] Adding ${ checked . length } NZB(s)` ) ; button . trigger ( 'nzb.pending' ) ; Promise . all ( checked . map ( ( i , el ) => { const check = $ ( el ) ; const id = check . val ( ) as string ; if ( /[a-d0-9\-]+/ . test ( id ) ) { const linkOpts = { url : getNzbUrl ( id ) } ; console . info ( `[NZB Unity] Adding URL` , linkOpts ) ; return Util . sendMessage ( { 'content.addUrl' : linkOpts } ) ; } else { return Promise . resolve ( ) ; } } ) ) . then ( ( results ) => { setTimeout ( ( ) => { if ( results . some ( r => r === false ) ) { button . trigger ( 'nzb.failure' ) ; } else { button . trigger ( 'nzb.success' ) ; } } , 1000 ) ; } ) ; } } ) . insertBefore ( el ) ; } ) ; } } $ ( ( $ ) => { const nzbIntegration = new NZBUnityTabulaRasa ( ) ; } ) ; undefined ;