transcript-table.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. let details_tt, select_tt, table_tt;
  2. function renderCues() {
  3. const selectedTrack = QId("js-video-player").textTracks[select_tt.selectedIndex];
  4. const cuesList = [...selectedTrack.cues];
  5. const is_automatic = cuesList[0].text.startsWith(" \n");
  6. // Firefox ignores cues starting with a blank line containing a space
  7. // Automatic captions contain such a blank line in the first cue
  8. let ff_bug = false;
  9. if (!cuesList[0].text.length) { ff_bug = true; is_automatic = true };
  10. let rows;
  11. function forEachCue(callback) {
  12. for (let i=0; i < cuesList.length; i++) {
  13. let txt, startTime = selectedTrack.cues[i].startTime;
  14. if (is_automatic) {
  15. // Automatic captions repeat content. The new segment is displayed
  16. // on the bottom row; the old one is displayed on the top row.
  17. // So grab the bottom row only. Skip every other cue because the bottom
  18. // row is empty.
  19. if (i % 2) continue;
  20. if (ff_bug && !selectedTrack.cues[i].text.length) {
  21. txt = selectedTrack.cues[i+1].text;
  22. } else {
  23. txt = selectedTrack.cues[i].text.split('\n')[1].replace(/<[\d:.]*?><c>(.*?)<\/c>/g, "$1");
  24. }
  25. } else {
  26. txt = selectedTrack.cues[i].text;
  27. }
  28. callback(startTime, txt);
  29. }
  30. }
  31. function createTimestampLink(startTime, txt, title=null) {
  32. a = document.createElement("a");
  33. a.appendChild(text(txt));
  34. a.href = "javascript:;"; // TODO: replace this with ?t parameter
  35. if (title) a.title = title;
  36. a.addEventListener("click", (e) => {
  37. QId("js-video-player").currentTime = startTime;
  38. })
  39. return a;
  40. }
  41. clearNode(table_tt);
  42. console.log("render cues..", selectedTrack.cues.length);
  43. if (Q("input#transcript-use-table").checked) {
  44. forEachCue((startTime, txt) => {
  45. let tr, td, a;
  46. tr = document.createElement("tr");
  47. td = document.createElement("td")
  48. td.appendChild(createTimestampLink(startTime, toTimestamp(startTime)));
  49. tr.appendChild(td);
  50. td = document.createElement("td")
  51. td.appendChild(text(txt));
  52. tr.appendChild(td);
  53. table_tt.appendChild(tr);
  54. });
  55. rows = table_tt.rows;
  56. }
  57. else {
  58. forEachCue((startTime, txt) => {
  59. span = document.createElement("span");
  60. let idx = txt.indexOf(" ", 1);
  61. let [firstWord, rest] = [txt.slice(0, idx), txt.slice(idx)];
  62. span.appendChild(createTimestampLink(startTime, firstWord, toTimestamp(startTime)));
  63. if (rest) span.appendChild(text(rest + " "));
  64. table_tt.appendChild(span);
  65. });
  66. rows = table_tt.childNodes;
  67. }
  68. let lastActiveRow = null;
  69. let row;
  70. function colorCurRow(e) {
  71. // console.log("cuechange:", e);
  72. let activeCueIdx = cuesList.findIndex((c) => c == selectedTrack.activeCues[0]);
  73. let activeRowIdx = is_automatic ? Math.floor(activeCueIdx / 2) : activeCueIdx;
  74. if (lastActiveRow) lastActiveRow.style.backgroundColor = "";
  75. if (activeRowIdx < 0) return;
  76. row = rows[activeRowIdx];
  77. row.style.backgroundColor = "#0cc12e42";
  78. lastActiveRow = row;
  79. }
  80. selectedTrack.addEventListener("cuechange", colorCurRow);
  81. }
  82. function loadCues() {
  83. const textTracks = QId("js-video-player").textTracks;
  84. const selectedTrack = textTracks[select_tt.selectedIndex];
  85. // See https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/mode
  86. // This code will (I think) make sure that the selected track's cues
  87. // are loaded even if the track subtitles aren't on (showing). Setting it
  88. // to hidden will load them.
  89. let selected_track_target_mode = "hidden";
  90. for (let track of textTracks) {
  91. // Want to avoid unshowing selected track if it's showing
  92. if (track.mode === "showing") selected_track_target_mode = "showing";
  93. if (track !== selectedTrack) track.mode = "disabled";
  94. }
  95. if (selectedTrack.mode == "disabled") {
  96. selectedTrack.mode = selected_track_target_mode;
  97. }
  98. let intervalID = setInterval(() => {
  99. if (selectedTrack.cues && selectedTrack.cues.length) {
  100. clearInterval(intervalID);
  101. renderCues();
  102. }
  103. }, 100);
  104. }
  105. window.addEventListener('DOMContentLoaded', function() {
  106. const textTracks = QId("js-video-player").textTracks;
  107. if (!textTracks.length) return;
  108. details_tt = Q("details#transcript-details");
  109. details_tt.addEventListener("toggle", () => {
  110. if (details_tt.open) loadCues();
  111. });
  112. select_tt = Q("select#select-tt");
  113. select_tt.selectedIndex = getDefaultTranscriptTrackIdx();
  114. select_tt.addEventListener("change", loadCues);
  115. table_tt = Q("table#transcript-table");
  116. table_tt.appendChild(text("loading..."));
  117. textTracks.addEventListener("change", (e) => {
  118. // console.log(e);
  119. let idx = getActiveTranscriptTrackIdx(); // sadly not provided by 'e'
  120. if (textTracks[idx].mode == "showing") {
  121. select_tt.selectedIndex = idx;
  122. loadCues();
  123. }
  124. else if (details_tt.open && textTracks[idx].mode == "disabled") {
  125. textTracks[idx].mode = "hidden"; // so we still receive 'oncuechange'
  126. }
  127. })
  128. Q("input#transcript-use-table").addEventListener("change", renderCues);
  129. });