import { Intent } from "../lib/common" ; import * as dlAll from "../lib/download_all/widgets" ; import * as mal from "../lib/MyAnimeList/widgets" ; import utilityBar from "../lib/utility_bar" ; import { loadSettings } from "../lib/utils" ; console . info ( "%c9anime Companion 1.0.0" , "color: orange; font-weight: bold;" ) ; let title = $ ( "h1.title" ) ; let serverDiv = $ ( "#servers" ) ; let animeName = title . text ( ) ; let animeId = $ ( "#movie" ) . data ( "id" ) ; let currentEpId = serverDiv . find ( ".episodes > li > a.active" ) . data ( "id" ) ; let currentEpNum = Number ( serverDiv . find ( ".episodes > li > a.active" ) . data ( "base" ) ) || 0 ; /**
 * Contains the callback functions which are executed when
 * the episode on the current page is changed via ajax.
 */ let epChangeCallbacks = [ ] ; /**
 * This part tracks when episodes are changed after the page loads.
 * Since the episodes are loaded via ajax, there is no direct way
 * to track them. Instead, we poll history.state every second
 * and if id was changed, callbacks in epChangeCallbacks are executed
 * with newEpId and newEpNum as the parameters.
 */ setInterval ( ( ) => { if ( history . state && history . state . name ) { let newEpId = history . state . name ; if ( newEpId !== currentEpId ) { let newEpNum = Number ( serverDiv . find ( ".episodes > li > a.active" ) . data ( "base" ) ) || 0 ; currentEpId = newEpId ; currentEpNum = newEpNum ; epChangeCallbacks . forEach ( el => { el . call ( null , newEpId , newEpNum ) ; } ) ; } } } , 1000 ) ; function registerRecent ( ) { chrome . runtime . sendMessage ( { animeId , animeName , epId : currentEpId , epNum : currentEpNum , intent : Intent . Recently_Watched_Add , url : $ ( "link[rel='canonical']" ) . attr ( "href" ) , } ) ; } registerRecent ( ) ; epChangeCallbacks . push ( ( ) => { console . info ( `%cUpdated recent to ${ currentEpNum }` , "color: yellow;" ) ; registerRecent ( ) ; } ) ; loadSettings ( [ "downloadAll" , "myAnimeList" , "remAds" , "remComments" , "remInfo" , "remSuggested" , "resPlayer" , "utilityBar" , ] ) . then ( settings => { if ( settings . remAds ) { console . info ( "%c[✔] Removing ads" , "color: lightgreen;" ) ; let adsSelectors = [ ".a_d" , ".ads" , ".sidebar" , ".pa" , ".myass" , ] ; for ( let i of adsSelectors ) { $ ( i ) . remove ( ) ; } $ ( "#movie" ) . find ( "div.container.player-wrapper > div > div.col-xs-24" ) . css ( { float : "none" } ) ; } if ( settings . resPlayer ) { console . info ( "%c[✔] Resizing player" , "color: lightgreen;" ) ; let player = $ ( "#player" ) ; player . css ( "maxHeight" , "500px" ) ; player . parent ( ) . css ( "width" , "100%" ) ; } if ( settings . remComments ) { console . info ( "%c[✔] Removing comments" , "color: lightgreen;" ) ; $ ( "#comment" ) . remove ( ) ; } if ( settings . remSuggested ) { console . info ( "%c[✔] Removing suggested" , "color: lightgreen;" ) ; $ ( "#movie" ) . find ( "div.widget-title" ) . filter ( ( index , el ) => { return $ ( el ) . text ( ) === "You might also like" ; } ) . parent ( ) . remove ( ) ; } if ( settings . remInfo ) { console . info ( "%c[✔] Removing info" , "color: lightgreen;" ) ; title . remove ( ) ; $ ( "#info" ) . remove ( ) ; } if ( settings . utilityBar ) { console . info ( "%c[✔] Attaching utility bar" , "color: lightgreen;" ) ; $ ( "#player" ) . parent ( ) . append ( utilityBar ( animeName ) ) ; } if ( settings . downloadAll ) { console . info ( "%c[✔] Attaching download all" , "color: lightgreen;" ) ; dlAll . setup ( { name : animeName , ts : $ ( "body" ) . data ( "ts" ) , } ) ; } if ( settings . myAnimeList ) { console . info ( "%c[✔] Attaching MyAnimeList" , "color: lightgreen;" ) ; mal . setup ( { animeName , } ) ; epChangeCallbacks . push ( mal . epTracker ) ; } } ) ;