From b3de26606dcdb374a98a494136d2c3f210fff838 Mon Sep 17 00:00:00 2001 From: zrose584 <57181548+zrose584@users.noreply.github.com> Date: Tue, 15 Sep 2020 20:32:12 +0200 Subject: handle firefox' VTT parsing bug from #15: "[..] Firefox's VTT parsing [ignores] newlines. So if the cue starts with a newline, that cue will have blank text (a corollary is that the first sentence uttered will fail to display in the automatic captions [..])." --- youtube/static/js/transcript-table.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'youtube/static/js') diff --git a/youtube/static/js/transcript-table.js b/youtube/static/js/transcript-table.js index acc2703..d0028f7 100644 --- a/youtube/static/js/transcript-table.js +++ b/youtube/static/js/transcript-table.js @@ -4,6 +4,8 @@ function renderCues() { var tt = Q("video").textTracks[select_tt.selectedIndex]; let cuesL = [...tt.cues]; var tt_type = cuesL[0].text.startsWith(" \n"); + let ff_bug = false; + if (!cuesL[0].text.length) { ff_bug = true; tt_type = true }; let rows; function forEachCue(cb) { @@ -11,7 +13,8 @@ function renderCues() { let txt, startTime = tt.cues[i].startTime; if (tt_type) { if (i % 2) continue; - txt = tt.cues[i].text.split('\n')[1].replace(/<[\d:.]*?>(.*?)<\/c>/g, "$1"); + if (ff_bug && !tt.cues[i].text.length) txt = tt.cues[i+1].text; + else txt = tt.cues[i].text.split('\n')[1].replace(/<[\d:.]*?>(.*?)<\/c>/g, "$1"); } else { txt = tt.cues[i].text; } @@ -52,7 +55,7 @@ function renderCues() { else { forEachCue((startTime, txt) => { span = document.createElement("span"); - var idx = txt.indexOf(" "); + var idx = txt.indexOf(" ", 1); var [firstWord, rest] = [txt.slice(0, idx), txt.slice(idx)]; span.appendChild(createA(startTime, firstWord, toMS(startTime))); @@ -90,8 +93,8 @@ function loadCues() { var iC = setInterval(() => { if (tt.cues && tt.cues.length) { - renderCues(); clearInterval(iC); + renderCues(); } }, 100); } -- cgit v1.2.3