11const { ElementFromFile, templatePath } = require ( "../utils" ) ;
22const { ipcRenderer } = require ( "electron" ) ;
33
4+ const config = require ( "./config" ) ;
5+
46function $ ( selector ) { return document . querySelector ( selector ) ; }
57
68const captionsSettingsButton = ElementFromFile (
79 templatePath ( __dirname , "captions-settings-template.html" )
810) ;
911
10- module . exports = ( options ) => {
11- document . addEventListener ( 'apiLoaded' , ( event ) => setup ( event , options ) , { once : true , passive : true } ) ;
12+ module . exports = ( ) => {
13+ document . addEventListener ( 'apiLoaded' , ( event ) => setup ( event . detail ) , { once : true , passive : true } ) ;
1214}
1315
14- function setup ( event , options ) {
15- const api = event . detail ;
16-
16+ function setup ( api ) {
1717 $ ( ".right-controls-buttons" ) . append ( captionsSettingsButton ) ;
1818
1919 let captionTrackList = api . getOption ( "captions" , "tracklist" ) ;
2020
21- $ ( "video" ) . addEventListener ( "srcChanged" , ( ) => {
22- if ( options . disableCaptions ) {
21+ $ ( "video" ) . addEventListener ( "srcChanged" , async ( ) => {
22+ if ( await config . get ( ' disableCaptions' ) ) {
2323 setTimeout ( ( ) => api . unloadModule ( "captions" ) , 100 ) ;
2424 captionsSettingsButton . style . display = "none" ;
2525 return ;
2626 }
2727
2828 api . loadModule ( "captions" ) ;
2929
30- setTimeout ( ( ) => {
30+ setTimeout ( async ( ) => {
3131 captionTrackList = api . getOption ( "captions" , "tracklist" ) ;
3232
33+ if ( await config . get ( "autoload" ) && await config . get ( "lastCaptionsCode" ) ) {
34+ api . setOption ( "captions" , "track" , {
35+ languageCode : await config . get ( "lastCaptionsCode" ) ,
36+ } ) ;
37+ }
38+
3339 captionsSettingsButton . style . display = captionTrackList ?. length
3440 ? "inline-block"
3541 : "none" ;
@@ -52,6 +58,7 @@ function setup(event, options) {
5258 if ( currentIndex === null ) return ;
5359
5460 const newCaptions = captionTrackList [ currentIndex ] ;
61+ config . set ( 'lastCaptionsCode' , newCaptions ?. languageCode ) ;
5562 if ( newCaptions ) {
5663 api . setOption ( "captions" , "track" , { languageCode : newCaptions . languageCode } ) ;
5764 } else {
0 commit comments