aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/static/js/transcript-table.js
diff options
context:
space:
mode:
authorzrose584 <57181548+zrose584@users.noreply.github.com>2020-09-12 13:22:46 +0200
committerzrose584 <57181548+zrose584@users.noreply.github.com>2020-09-12 13:22:46 +0200
commit0be0a59a2a3eef8da75f2bae57df72526598b2bf (patch)
tree0945d5e0bf85f82ca56d82fe607a73d10df99773 /youtube/static/js/transcript-table.js
parent57978485ee80e13c6ac1a9f6377934be32c4c67a (diff)
downloadyt-local-0be0a59a2a3eef8da75f2bae57df72526598b2bf.tar.lz
yt-local-0be0a59a2a3eef8da75f2bae57df72526598b2bf.tar.xz
yt-local-0be0a59a2a3eef8da75f2bae57df72526598b2bf.zip
transcript-table: show transcript as contiguous text by default
Diffstat (limited to 'youtube/static/js/transcript-table.js')
-rw-r--r--youtube/static/js/transcript-table.js77
1 files changed, 52 insertions, 25 deletions
diff --git a/youtube/static/js/transcript-table.js b/youtube/static/js/transcript-table.js
index fac33da..acc2703 100644
--- a/youtube/static/js/transcript-table.js
+++ b/youtube/static/js/transcript-table.js
@@ -3,39 +3,64 @@ var details_tt, select_tt, table_tt;
function renderCues() {
var tt = Q("video").textTracks[select_tt.selectedIndex];
let cuesL = [...tt.cues];
-
- clearNode(table_tt);
- console.log("render cues..", tt.cues.length);
-
var tt_type = cuesL[0].text.startsWith(" \n");
- for (let i=0; i < cuesL.length; i++) {
- 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>(.*?)<\/c>/g, "$1");
- } else {
- txt = tt.cues[i].text;
+ let rows;
+
+ function forEachCue(cb) {
+ for (let i=0; i < cuesL.length; i++) {
+ 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>(.*?)<\/c>/g, "$1");
+ } else {
+ txt = tt.cues[i].text;
+ }
+ cb(startTime, txt);
}
+ }
- let tr, td, a;
- tr = document.createElement("tr");
-
- td = document.createElement("td")
+ function createA(startTime, txt, title=null) {
a = document.createElement("a");
- a.appendChild(text(toMS(startTime)));
+ a.appendChild(text(txt));
a.href = "javascript:;"; // TODO: replace this with ?t parameter
+ if (title) a.title = title;
a.addEventListener("click", (e) => {
Q("video").currentTime = startTime;
})
- td.appendChild(a);
- tr.appendChild(td);
-
- td = document.createElement("td")
- td.appendChild(text(txt));
- tr.appendChild(td);
+ return a;
+ }
- table_tt.appendChild(tr);;
- };
+ clearNode(table_tt);
+ console.log("render cues..", tt.cues.length);
+ if (Q("input#transcript-use-table").checked) {
+ forEachCue((startTime, txt) => {
+ let tr, td, a;
+ tr = document.createElement("tr");
+
+ td = document.createElement("td")
+ td.appendChild(createA(startTime, toMS(startTime)));
+ tr.appendChild(td);
+
+ td = document.createElement("td")
+ td.appendChild(text(txt));
+ tr.appendChild(td);
+
+ table_tt.appendChild(tr);
+ });
+ rows = table_tt.rows;
+ }
+ else {
+ forEachCue((startTime, txt) => {
+ span = document.createElement("span");
+ var idx = txt.indexOf(" ");
+ var [firstWord, rest] = [txt.slice(0, idx), txt.slice(idx)];
+
+ span.appendChild(createA(startTime, firstWord, toMS(startTime)));
+ if (rest) span.appendChild(text(rest + " "));
+ table_tt.appendChild(span);
+ });
+ rows = table_tt.childNodes;
+ }
var lastActiveRow = null;
function colorCurRow(e) {
@@ -45,7 +70,7 @@ function renderCues() {
if (lastActiveRow) lastActiveRow.style.backgroundColor = "";
if (idxT < 0) return;
- var row = table_tt.rows[idxT];
+ var row = rows[idxT];
row.style.backgroundColor = "#0cc12e42";
lastActiveRow = row;
}
@@ -99,5 +124,7 @@ window.addEventListener('DOMContentLoaded', function() {
}
})
+ Q("input#transcript-use-table").addEventListener("change", renderCues);
+
Q(".side-videos").prepend(details_tt);
});