diff options
Diffstat (limited to 'src/js/captions.js')
-rw-r--r-- | src/js/captions.js | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/js/captions.js b/src/js/captions.js index ae4642aa..f7c24534 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -85,7 +85,6 @@ const captions = { const browserLanguages = navigator.languages || [navigator.language || navigator.userLanguage || 'en']; const languages = dedupe(browserLanguages.map(language => language.split('-')[0])); - let language = (this.storage.get('language') || this.config.captions.language || 'auto').toLowerCase(); // Use first browser language when language is 'auto' @@ -124,19 +123,21 @@ const captions = { // Handle tracks (add event listener and "pseudo"-default) if (this.isHTML5 && this.isVideo) { - tracks.filter(track => !meta.get(track)).forEach(track => { - this.debug.log('Track added', track); - // Attempt to store if the original dom element was "default" - meta.set(track, { - default: track.mode === 'showing', + tracks + .filter(track => !meta.get(track)) + .forEach(track => { + this.debug.log('Track added', track); + // Attempt to store if the original dom element was "default" + meta.set(track, { + default: track.mode === 'showing', + }); + + // Turn off native caption rendering to avoid double captions + Object.assign(track, { mode: 'hidden' }); + + // Add event listener for cue changes + on.call(this, track, 'cuechange', () => captions.updateCues.call(this)); }); - - // Turn off native caption rendering to avoid double captions - track.mode = 'hidden'; - - // Add event listener for cue changes - on.call(this, track, 'cuechange', () => captions.updateCues.call(this)); - }); } // Update language first time it matches, or if the previous matching track was removed @@ -164,7 +165,6 @@ const captions = { const { toggled } = this.captions; // Current state const activeClass = this.config.classNames.captions.active; - // Get the next state // If the method is called without parameter, toggle based on current value const active = is.nullOrUndefined(input) ? !toggled : input; @@ -300,10 +300,12 @@ const captions = { const sortIsDefault = track => Number((this.captions.meta.get(track) || {}).default); const sorted = Array.from(tracks).sort((a, b) => sortIsDefault(b) - sortIsDefault(a)); let track; + languages.every(language => { - track = sorted.find(track => track.language === language); + track = sorted.find(t => t.language === language); return !track; // Break iteration if there is a match }); + // If no match is found but is required, get first return track || (force ? sorted[0] : undefined); }, @@ -360,6 +362,7 @@ const captions = { // Get cues from track if (!cues) { const track = captions.getCurrentTrack.call(this); + cues = Array.from((track || {}).activeCues || []) .map(cue => cue.getCueAsHTML()) .map(getHTML); |