Skip to content

Commit ee49d6b

Browse files
committed
Customizable subtitle bottom margin
1 parent e8f9b14 commit ee49d6b

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

chrome/player/options/defaults/DefaultSubtitlesSettings.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const DefaultSubtitlesSettings = {
77
outlineColor: 'rgb(0,0,0)',
88
outlineWidth: '0px',
99
defaultLanguage: 'en',
10+
bottomMargin: '40px',
1011
};
1112

1213
export const SubtitleSettingsConfigData = {
@@ -42,4 +43,7 @@ export const SubtitleSettingsConfigData = {
4243
defaultLanguage: {
4344
type: 'string',
4445
},
46+
bottomMargin: {
47+
type: 'string',
48+
},
4549
};

chrome/player/players/yt/YTPlayer.mjs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,29 @@ export default class YTPlayer extends DashPlayer {
208208
await super.setSource(this.source);
209209

210210
if (this.videoInfo.captions?.caption_tracks) {
211-
this.videoInfo.captions.caption_tracks.forEach(async (track) => {
211+
// Sort this.videoInfo.captions.caption_tracks so that the default language is first
212+
let defLang = 'en';
213+
const subtitlesSettings = await Utils.getSubtitlesSettingsFromStorage();
214+
if (subtitlesSettings.defaultLanguage) {
215+
defLang = subtitlesSettings.defaultLanguage;
216+
}
217+
const tracks = this.videoInfo.captions.caption_tracks.slice();
218+
tracks.sort((a, b) => {
219+
const aMatches = a.language_code.substring(0, defLang.length) === defLang;
220+
const bMatches = b.language_code.substring(0, defLang.length) === defLang;
221+
if (aMatches && !bMatches) return -1;
222+
if (!aMatches && bMatches) return 1;
223+
if (aMatches && bMatches) {
224+
// if one is auto (a.kind === 'asr') and the other is not, prefer the non-auto
225+
const aIsAuto = a.kind === 'asr';
226+
const bIsAuto = b.kind === 'asr';
227+
if (aIsAuto && !bIsAuto) return 1;
228+
if (!aIsAuto && bIsAuto) return -1;
229+
}
230+
return 0;
231+
});
232+
233+
const promises = tracks.map(async (track) => {
212234
let url = track.base_url;
213235
// if po token exists, add it to the url
214236
if (this.ytclient.session.content_token) {
@@ -223,7 +245,15 @@ export default class YTPlayer extends DashPlayer {
223245

224246
const subTrack = new SubtitleTrack(label, language);
225247
await subTrack.loadURL(url);
226-
this.client.loadSubtitleTrack(subTrack, true);
248+
return subTrack;
249+
});
250+
251+
await Promise.allSettled(promises).then((results) => {
252+
results.forEach((result) => {
253+
if (result.status === 'fulfilled' && result.value) {
254+
this.client.loadSubtitleTrack(result.value, true);
255+
}
256+
});
227257
});
228258
}
229259

chrome/player/ui/subtitles/SubtitlesManager.mjs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,22 @@ export class SubtitlesManager extends EventEmitter {
4343
}
4444

4545
const defLang = this.settingsManager.getSettings().defaultLanguage;
46-
if (autoset && this.activeTracks.length === 0 && this.client.options.autoEnableBestSubtitles) {
46+
if (autoset && this.activeTracks.length <= 1 && this.client.options.autoEnableBestSubtitles) {
4747
if (subtitleTrack.language && subtitleTrack.language.substring(0, defLang.length) === defLang) {
48-
this.activateTrack(subtitleTrack);
48+
// check if active tracks exist with same language
49+
const existingActive = this.activeTracks.find((t) => {
50+
return t.language && subtitleTrack.language && t.language.substring(0, defLang.length) === defLang;
51+
});
52+
if (!existingActive) {
53+
this.activateTrack(subtitleTrack);
54+
} else {
55+
// Check if existing has "auto" in label, if new one doesn't, prefer new one
56+
if (existingActive.label && existingActive.label.toLowerCase().includes('auto') &&
57+
subtitleTrack.label && !subtitleTrack.label.toLowerCase().includes('auto')) {
58+
this.deactivateTrack(existingActive);
59+
this.activateTrack(subtitleTrack);
60+
}
61+
}
4962
}
5063
}
5164

@@ -690,6 +703,8 @@ export class SubtitlesManager extends EventEmitter {
690703

691704
if (subtitlesVisible) {
692705
DOMElements.subtitlesContainer.style.display = '';
706+
const margin = this.settingsManager.getSettings().bottomMargin;
707+
DOMElements.subtitlesContainer.style.bottom = margin === '40px' ? '' : this.settingsManager.getSettings().bottomMargin;
693708
} else {
694709
DOMElements.subtitlesContainer.style.display = 'none';
695710
}

0 commit comments

Comments
 (0)