Skip to content

Commit

Permalink
Add extra languages to secondary subs list when they are activated (#36)
Browse files Browse the repository at this point in the history
This makes extra subtitle appears as a secondary subtitle option after user selects it as primary subtitle
  • Loading branch information
hyww authored Dec 16, 2022
1 parent 6c14c18 commit aeebe4b
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/nflxmultisubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ class DummySubtitle extends SubtitleBase {
}
}

// subtitle with no download urls
class DehydratedSubtitle extends SubtitleBase {
constructor(...args) {
super(...args);
}

activate() {
this.active = true;
return Promise.resolve();
}
}

class TextSubtitle extends SubtitleBase {
constructor(...args) {
super(...args);
Expand Down Expand Up @@ -419,6 +431,9 @@ class SubtitleFactory {
const lang = track.languageDescription + (isCaption ? ' [CC]' : '');
const bcp47 = track.language;

if (!track.hydrated) {
return new DehydratedSubtitle(lang, bcp47);
}
if (isImageBased) {
return this._buildImageBased(track, lang, bcp47, isCaption);
}
Expand Down Expand Up @@ -499,6 +514,17 @@ const buildSubtitleList = textTracks => {
return subs.concat(dummy);
};

// textTracks: manifest.textTracks
const updateSubtitleList = (textTracks, textTrackId) => {
const track = textTracks.find(t => t.new_track_id == textTrackId),
sub = SubtitleFactory.build(track),
index = gSubtitles.findIndex(s => s.lang == sub.lang);
if (gSubtitles[index] instanceof DehydratedSubtitle && sub !== null) {
gSubtitles[index] = sub;
gSubtitleMenu && gSubtitleMenu.render();
}
};

////////////////////////////////////////////////////////////////////////////////

const SUBTITLE_LIST_CLASSNAME = 'nflxmultisubs-subtitle-list';
Expand Down Expand Up @@ -541,6 +567,7 @@ class SubtitleMenu {

const listElem = document.createElement('ul');
gSubtitles.forEach((sub, id) => {
if (sub instanceof DehydratedSubtitle) return;
let item = document.createElement('li');
item.classList.add(this.style.li);
if (sub.active) {
Expand Down Expand Up @@ -1251,7 +1278,8 @@ class NflxMultiSubsManager {

const movieChanged = manifest.movieId !== this.lastMovieId;
if (!movieChanged) {
//console.log(`Ignored: manifest ${manifest.movieId} already loaded`);
updateSubtitleList(manifest.timedtexttracks, manifest.recommendedMedia.timedTextTrackId);
console.log(`Manifest ${manifest.movieId} updated`);
return;
}

Expand Down Expand Up @@ -1396,4 +1424,4 @@ window.addEventListener('keyup', (event) => {

primary.style.visibility = secondary.style.visibility = (visible) ? 'hidden' : 'visible';
}
}, true);
}, true);

0 comments on commit aeebe4b

Please sign in to comment.