/**
 * Konichiwa~
 *
 * => Part of Content Script
 * This is responsible for the Download All widgets/components.
 */ declare function require ( arg ) ; import { DownloadMethod , DownloadMethodKeys , DownloadQuality , DownloadQualityKeys , IEpisode , Intent , IRuntimeMessage , Server , } from "../common" ; import * as utils from "../utils" ; let server = Server . Default ; let method = DownloadMethod . Browser ; let isDownloading = false ; let ts = "" ; let animeName = "" ; let serverId = "" ; const selectors = { closeBtn : ".nac__modal__close" , copyLinks : "#nac__dl-all__copy-links" , dlBtn : ".nac__dl-all__btn" , download : "#nac__dl-all__download" , epModal : "#nac__dl-all__ep-modal" , links : "#nac__dl-all__links" , linksModal : "#nac__dl-all__links-modal" , method : "#nac__dl-all__method" , quality : "#nac__dl-all__quality" , selectAll : "#nac__dl-all__select-all" , status : "#nac__dl-all__status" , statusBar : ".nac__dl-all__status-bar" , } ; interface ISetupOptions { name : string ; ts : string ; } export function setup ( options ) { animeName = options . name ; ts = options . ts ; let body = $ ( "body" ) ; let servers = $ ( ".server.row > label" ) ; $ ( "#servers" ) . prepend ( statusBar ( ) ) ; body . append ( epModal ( ) ) ; body . append ( linksModal ( ) ) ; for ( let s of servers ) { let serverLabel = $ ( s ) . text ( ) ; if ( /Server\s+F/i . test ( serverLabel ) ) { $ ( s ) . append ( downloadBtn ( Server . Default ) ) ; } else if ( /Server\s+G/i . test ( serverLabel ) ) { $ ( s ) . append ( downloadBtn ( Server . Default ) ) ; } else if ( /RapidVideo/i . test ( serverLabel ) ) { $ ( s ) . append ( downloadBtn ( Server . RapidVideo ) ) ; } } } function showModal ( selector ) { let modal = $ ( selector ) ; $ ( modal ) . show ( ) ; $ ( modal ) . find ( ".container" ) . addClass ( "fade_in" ) ; $ ( "body" ) . css ( "overflow" , "hidden" ) ; setTimeout ( ( ) => { $ ( modal ) . find ( ".container" ) . removeClass ( "fade_in" ) ; } , 500 ) ; } function hideModal ( selector ) { let modal = $ ( selector ) ; $ ( modal ) . find ( ".container" ) . addClass ( "fade_out" ) ; setTimeout ( ( ) => { $ ( modal ) . find ( ".container" ) . removeClass ( "fade_out" ) ; $ ( modal ) . hide ( ) ; $ ( "body" ) . css ( "overflow" , "auto" ) ; } , 500 ) ; } function shakeModal ( selector ) { let modal = $ ( selector ) ; $ ( modal ) . find ( ".container" ) . addClass ( "shake" ) ; setTimeout ( ( ) => { $ ( modal ) . find ( ".container" ) . removeClass ( "shake" ) ; } , 820 ) ; } function disableInputs ( ) { $ ( selectors . dlBtn ) . attr ( "disabled" , "disabled" ) ; } function enableInputs ( ) { $ ( selectors . dlBtn ) . removeAttr ( "disabled" ) ; } export function statusBar ( ) { return `
    <div class="nac__dl-all__status-bar" style="display: none;">
        <span>Status:</span>
        <div id="nac__dl-all__status">ready to download...</div>
    </div>` ; } param { Server } targetServer returns export function downloadBtn ( targetServer ) { let btn = $ ( `<button data-type="${ targetServer }" class="nac__dl-all__btn">Download</button>` ) ; btn . on ( "click" , e => { let episodes = [ ] ; server = $ ( e . currentTarget ) . data ( "type" ) ; let epLinks = $ ( e . currentTarget ) . parents ( ".server.row" ) . find ( ".episodes > li > a" ) ; for ( let ep of epLinks ) { let id = $ ( ep ) . data ( "id" ) ; let num = $ ( ep ) . data ( "base" ) ; if ( id && num ) { episodes . push ( { id , num : utils . pad ( num ) , } ) ; } } let modalBody = $ ( selectors . epModal ) . find ( ".body" ) ; const episodesContainer = $ ( `<div class="nac__dl-all__episodes-container"></div>` ) ; modalBody . empty ( ) ; for ( let ep of episodes ) { let epSpan = $ ( `<span class="nac__dl-all__episode">
                    <input type="checkbox" id="${ ep . id }" data-num="${ ep . num }">
                    <label for="${ ep . id }">${ animeName }: Ep. ${ ep . num }</label>
                </span>` ) ; episodesContainer . append ( epSpan ) ; } modalBody . append ( episodesContainer ) ; serverId = btn . parents ( ".server.row" ) . data ( "id" ) ; showModal ( selectors . epModal ) ; } ) ; return btn ; } returns export function linksModal ( ) { let template = require ( "html-loader!../../templates/dlAll_linksModal.html" ) ; let modal = $ ( template ) ; let clipboardIcon = chrome . extension . getURL ( "images/clipboard.png" ) ; modal . find ( selectors . copyLinks ) . find ( "img" ) . attr ( "src" , clipboardIcon ) ; modal . on ( "click" , e => { if ( e . target === modal [ 0 ] ) { hideModal ( selectors . linksModal ) ; } } ) ; modal . find ( selectors . closeBtn ) . on ( "click" , e => { hideModal ( selectors . linksModal ) ; } ) ; modal . find ( selectors . copyLinks ) . on ( "click" , ( ) => { $ ( selectors . links ) . select ( ) ; document . execCommand ( "copy" ) ; } ) ; modal . hide ( ) ; return modal ; } returns export function epModal ( ) { let template = require ( "html-loader!../../templates/dlAll_epModal.html" ) ; let modal = $ ( template ) ; let lastQuality = localStorage . getItem ( "9AC__lastDownloadQuality" ) ; if ( lastQuality ) { modal . find ( "#nac__dl-all__quality" ) . val ( lastQuality ) ; } modal . find ( ".title" ) . text ( `Download ${ animeName } episodes:` ) ; modal . on ( "click" , e => { if ( e . target === modal [ 0 ] ) { hideModal ( selectors . epModal ) ; } } ) ; modal . find ( selectors . closeBtn ) . on ( "click" , e => { hideModal ( selectors . epModal ) ; } ) ; modal . find ( selectors . selectAll ) . on ( "click" , ( ) => { $ ( selectors . epModal ) . find ( ".body input[type='checkbox']" ) . prop ( "checked" , true ) ; } ) ; modal . find ( selectors . download ) . on ( "click" , ( ) => { if ( ! isDownloading ) { let selectedEpisodes = [ ] ; let quality = DownloadQuality [ $ ( selectors . quality ) . val ( ) as DownloadQualityKeys ] || DownloadQuality [ "360p" ] ; method = DownloadMethod [ $ ( selectors . method ) . val ( ) as DownloadMethodKeys ] || DownloadMethod . Browser ; $ ( selectors . epModal ) . find ( ".body input[type='checkbox']:checked" ) . each ( ( i , el ) => { selectedEpisodes . push ( { id : $ ( el ) . attr ( "id" ) || "" , num : $ ( el ) . data ( "num" ) , } ) ; } ) ; if ( selectedEpisodes . length > 0 ) { isDownloading = true ; disableInputs ( ) ; hideModal ( selectors . epModal ) ; $ ( selectors . statusBar ) . slideDown ( ) ; chrome . runtime . sendMessage ( { animeName , baseUrl : window . location . origin , server , intent : Intent . Download_All , method , quality , selectedEpisodes , serverId , ts , } ) ; localStorage . setItem ( "9AC__lastDownloadQuality" , DownloadQuality [ quality ] ) ; } else { shakeModal ( selectors . epModal ) ; } } } ) ; modal . hide ( ) ; return modal ; } /**
 * This part will notify us when the downloads are complete.
 */ chrome . runtime . onMessage . addListener ( ( message ) => { switch ( message . intent ) { case Intent . Download_Complete : if ( method === DownloadMethod . External && message . links ) { $ ( selectors . links ) . text ( message . links ) ; showModal ( selectors . linksModal ) ; } $ ( selectors . statusBar ) . slideUp ( ) ; isDownloading = false ; enableInputs ( ) ; break ; case Intent . Download_Status : $ ( selectors . status ) . text ( message . status ) ; break ; default : break ; } } ) ;