ydshieh HF staff commited on
Commit
2c4a6b5
1 Parent(s): 3d3f426

Add original transcript-tracer

Browse files
Files changed (1) hide show
  1. transcript-tracer.js +463 -0
transcript-tracer.js ADDED
@@ -0,0 +1,463 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+
3
+ © 2023 Samuel Bradshaw
4
+ https://github.com/samuelbradshaw/transcript-tracer-js
5
+
6
+ */
7
+
8
+
9
+ // Global state variables
10
+ var ttIsInitialized = false;
11
+ var ttTranscripts;
12
+ var ttMediaPlayers;
13
+ var ttActivePlayer;
14
+ var ttLinkedDataByMediaUrl = {};
15
+
16
+ // Configuration variables (see README.md)
17
+ var ttBlockSelector = null;
18
+ var ttPhraseSelector = null;
19
+ var ttAlignmentFuzziness = 0;
20
+ var ttTimeOffset = 0;
21
+ var ttAutoScroll = null;
22
+ var ttClickable = false;
23
+
24
+
25
+ // Prepare transcripts and media players
26
+ function loadTranscriptTracer(options=null) {
27
+ if (document.readyState == 'loading') {
28
+ // Wait for document to load
29
+ document.addEventListener('DOMContentLoaded', function() {
30
+ loadTranscriptTracer(options);
31
+ });
32
+ return;
33
+ }
34
+
35
+ // Save user-provided options to configuration variables
36
+ if (options) {
37
+ if ('blockSelector' in options) ttBlockSelector = options.blockSelector;
38
+ if ('phraseSelector' in options) ttPhraseSelector = options.phraseSelector;
39
+ if ('alignmentFuzziness' in options) ttAlignmentFuzziness = options.alignmentFuzziness;
40
+ if ('timeOffset' in options) ttTimeOffset = options.timeOffset;
41
+ if ('autoScroll' in options) ttAutoScroll = options.autoScroll;
42
+ if ('clickable' in options) ttClickable = options.clickable;
43
+ }
44
+
45
+ // Reset Transcript Tracer to prevent unexpected behavior if loadTranscriptTracer() is called more than once
46
+ if (ttIsInitialized) {
47
+ if (ttTranscripts) for (const transcript of ttTranscripts) {
48
+ unlinkTranscript(transcript);
49
+ transcript.dataset.ttTranscript = '';
50
+ }
51
+ ttTranscripts = null;
52
+ ttMediaPlayers = null;
53
+ ttActivePlayer = null;
54
+ ttLinkedDataByMediaUrl = {};
55
+ document.querySelectorAll('.tt-word, .tt-whitespace').forEach(element => {
56
+ element.outerHTML = element.innerHTML;
57
+ });
58
+ }
59
+
60
+ // Set a few global state variables
61
+ ttIsInitialized = true;
62
+ ttTranscripts = document.getElementsByClassName('tt-transcript');
63
+ ttMediaPlayers = document.querySelectorAll('audio, video');
64
+
65
+ // Prepare transcript for alignment by adding spans and classes
66
+ for (let t = 0; t < ttTranscripts.length; t++) {
67
+ var transcript = ttTranscripts[t];
68
+ // Skip transcript if it's not linked to any media files
69
+ if (!transcript.dataset.ttMediaUrls) continue;
70
+
71
+ transcript.dataset.ttTranscript = t;
72
+
73
+ // Loop through text nodes to add spans (tt-word, tt-whitespace)
74
+ // Method of iterating text nodes thanks to https://stackoverflow.com/a/34700627/1349044
75
+ var iter = document.createNodeIterator(transcript, NodeFilter.SHOW_TEXT);
76
+ var textNode;
77
+ while (textNode = iter.nextNode()) {
78
+ const text = textNode.textContent;
79
+ if (text.replace(/\s/g, '').length != 0) {
80
+ var spannedText = '<span class="tt-word">' + text.replace(/(\s+)/g, '</span><span class="tt-whitespace">$1</span><span class="tt-word">') + '</span>';
81
+ spannedText = spannedText.replaceAll('<span class="tt-word"></span>', '');
82
+
83
+ // Replace text node with spanned text
84
+ const template = document.createElement('template');
85
+ template.innerHTML = spannedText;
86
+ textNode.parentNode.insertBefore(template.content, textNode);
87
+ textNode.parentNode.removeChild(textNode);
88
+ }
89
+ }
90
+ }
91
+
92
+ for (const mediaPlayer of ttMediaPlayers) {
93
+ // Link related transcript(s)
94
+ linkTranscripts(mediaPlayer);
95
+
96
+ // Add event listener to media player
97
+ mediaPlayer.addEventListener('play', function(e) {
98
+ if (ttActivePlayer != e.currentTarget) {
99
+ if (ttActivePlayer) {
100
+ ttActivePlayer.pause();
101
+ ttActivePlayer.removeEventListener('timeupdate', ttTimeUpdate);
102
+ }
103
+ ttActivePlayer = e.currentTarget;
104
+ if (ttCurrentTranscript) clearHighlightedWords(ttCurrentTranscript);
105
+ if (ttCurrentEvent) ttCurrentEvent = null;
106
+ }
107
+ ttActivePlayer.addEventListener('timeupdate', ttTimeUpdate);
108
+ var currentTranscript = ttTranscripts[ttLinkedDataByMediaUrl[ttActivePlayer.dataset.ttLinkedMediaUrl].transcriptIndex];
109
+ currentTranscript.dataset.ttCurrentMediaUrl = ttActivePlayer.dataset.ttLinkedMediaUrl;
110
+ });
111
+ mediaPlayer.addEventListener('ended', function(e) {
112
+ if (ttCurrentTranscript) clearHighlightedWords(ttCurrentTranscript);
113
+ if (ttCurrentEvent) ttCurrentEvent = null;
114
+ });
115
+ }
116
+ }
117
+
118
+
119
+ // Link media player to relevant transcripts
120
+ function linkTranscripts(mediaPlayer) {
121
+ var trackElement = mediaPlayer.querySelector('track[kind="metadata"]')
122
+
123
+ var mediaPlayerSourceUrls = [];
124
+ var mediaPlayerSrc = mediaPlayer.getAttribute('src');
125
+ var mediaPlayerSourceElements = mediaPlayer.querySelectorAll('source');
126
+ if (mediaPlayerSrc) mediaPlayerSourceUrls.push(mediaPlayerSrc);
127
+ if (mediaPlayerSourceElements) for (const s of mediaPlayerSourceElements) mediaPlayerSourceUrls.push(s.src);
128
+
129
+ // If there's nothing to link, return
130
+ if (!trackElement || !trackElement.getAttribute('src') || mediaPlayerSourceUrls.length == 0) return;
131
+
132
+ // Fetch WebVTT content and link related transcripts
133
+ for (const transcript of ttTranscripts) {
134
+ for (const mediaUrl of mediaPlayerSourceUrls) {
135
+ if (transcript.dataset.ttMediaUrls.includes(mediaUrl)) {
136
+ mediaPlayer.dataset.ttLinkedMediaUrl = mediaUrl;
137
+ unlinkTranscript(transcript);
138
+ fetch(trackElement.src)
139
+ .then(r => r.text())
140
+ .then(vttContent => linkTranscript(mediaPlayer, vttContent, transcript));
141
+ break;
142
+ }
143
+ }
144
+ }
145
+
146
+ function linkTranscript(mediaPlayer, vttContent, transcript) {
147
+ var wordTimings = parseVttToWordTimings(vttContent);
148
+ transcript.dataset.ttCurrentMediaUrl = mediaPlayer.dataset.ttLinkedMediaUrl;
149
+
150
+ function normalizedWord(word) {
151
+ // Convert to lowercase, normalize, and remove anything that's not a letter or number
152
+ return word.toLowerCase().normalize('NFD').replace(/[^\p{L}\p{N}]/gu, '');
153
+ }
154
+
155
+ // Add metadata to block and phrase containers (if ttBlockSelector and ttPhraseSelector are defined)
156
+ var blockContainers = ttBlockSelector ? transcript.querySelectorAll(ttBlockSelector) : [];
157
+ for (let c = 0; c < blockContainers.length; c++) blockContainers[c].dataset.ttBlock = c;
158
+ var phraseContainers = ttPhraseSelector ? transcript.querySelectorAll(ttPhraseSelector) : [];
159
+ for (let c = 0; c < phraseContainers.length; c++) phraseContainers[c].dataset.ttPhrase = c;
160
+
161
+ // Add metadata to each word span, and build timed events list
162
+ var timedEvents = [];
163
+ var wordTimingsIndex = 0;
164
+ var wordSpans = transcript.getElementsByClassName('tt-word');
165
+ for (let s = 0; s < wordSpans.length; s++) {
166
+ var span = wordSpans[s];
167
+
168
+ // Find the next word timing object that matches the current span's text
169
+ var initialWordTimingsIndex = wordTimingsIndex;
170
+ var maxFuzzyWordTimingsIndex = Math.min(wordTimingsIndex + ttAlignmentFuzziness, wordTimings.length - 1);
171
+ while (normalizedWord(span.innerText) != normalizedWord(wordTimings[wordTimingsIndex].text) && wordTimingsIndex <= maxFuzzyWordTimingsIndex) {
172
+ wordTimingsIndex += 1;
173
+ }
174
+ if (normalizedWord(span.innerText) != normalizedWord(wordTimings[wordTimingsIndex].text)) {
175
+ // Could not find matching word within the fuzziness range
176
+ wordTimingsIndex = initialWordTimingsIndex;
177
+ continue;
178
+ }
179
+
180
+ // Get the block, phrase, and word index
181
+ blockIndex = ttBlockSelector ? (span.closest(ttBlockSelector)?.dataset?.ttBlock ?? null) : wordTimings[wordTimingsIndex].blockIndex;
182
+ phraseIndex = ttPhraseSelector ? (span.closest(ttPhraseSelector)?.dataset?.ttPhrase ?? null) : wordTimings[wordTimingsIndex].phraseIndex;
183
+ wordIndex = wordTimings[wordTimingsIndex].wordIndex;
184
+
185
+ // Add block, phrase, and word index as metadata on the span
186
+ span.dataset.ttBlock = blockIndex;
187
+ span.dataset.ttPhrase = phraseIndex;
188
+ span.dataset.ttWord = wordIndex;
189
+
190
+ // Add timed event to timed events list
191
+ if (timedEvents.length != 0 && wordTimings[wordTimingsIndex].startSeconds == timedEvents[timedEvents.length-1].seconds) {
192
+ timedEvents[timedEvents.length-1].currentWordIndexes.push(wordIndex);
193
+ } else {
194
+ timedEvents.push({
195
+ 'seconds': wordTimings[wordTimingsIndex].startSeconds,
196
+ 'currentWordIndexes': [wordIndex],
197
+ 'phraseIndex': phraseIndex,
198
+ 'blockIndex': blockIndex,
199
+ });
200
+ }
201
+
202
+ wordTimingsIndex += 1;
203
+ }
204
+
205
+ // For a given element, find the first parent element containing relevant children
206
+ function findRelevantParent(startingElement, endingElement, childSelector, relevantChildSelector) {
207
+ var currentElement = startingElement;
208
+ while (currentElement && currentElement != endingElement) {
209
+ var currentElement = currentElement.parentElement;
210
+ var children = currentElement.querySelectorAll(childSelector);
211
+ var relevantChildren = document.querySelectorAll(relevantChildSelector);
212
+ if (children.length == relevantChildren.length) {
213
+ // Relevant parent found
214
+ return currentElement;
215
+ } else if (children.length > relevantChildren.length) {
216
+ // Failed to find a relevant parent
217
+ break;
218
+ }
219
+ }
220
+ return null;
221
+ }
222
+
223
+ // Add metadata to block and phrase containers (if ttBlockSelector and ttPhraseSelector aren't defined)
224
+ if (!ttBlockSelector) {
225
+ var count = wordTimings[wordTimings.length-1].blockIndex + 1;
226
+ for (let c = 0; c < count; c++) {
227
+ var startingElement = document.querySelector(`[data-tt-block="${c}"]`);
228
+ var blockContainer = findRelevantParent(startingElement, transcript, '[data-tt-word]', `[data-tt-word][data-tt-block="${c}"]`);
229
+ if (blockContainer) blockContainer.dataset.ttBlock = c;
230
+ }
231
+ }
232
+ if (!ttPhraseSelector) {
233
+ var count = wordTimings[wordTimings.length-1].phraseIndex + 1;
234
+ for (let c = 0; c < count; c++) {
235
+ var startingElement = document.querySelector(`[data-tt-phrase="${c}"]`);
236
+ var phraseContainer = findRelevantParent(startingElement, transcript, '[data-tt-word]', `[data-tt-word][data-tt-phrase="${c}"]`);
237
+ if (phraseContainer) phraseContainer.dataset.ttPhrase = c;
238
+ }
239
+ }
240
+
241
+ // Sort timed events list by time
242
+ timedEvents = timedEvents.sort(function(a, b) {
243
+ return a.seconds - b.seconds;
244
+ })
245
+
246
+ // Add reference data to ttLinkedDataByMediaUrl
247
+ var transcriptIndex = parseInt(transcript.dataset.ttTranscript);
248
+ ttLinkedDataByMediaUrl[mediaPlayer.dataset.ttLinkedMediaUrl] = {
249
+ 'transcriptIndex': transcriptIndex,
250
+ 'wordTimings': wordTimings,
251
+ 'timedEvents': timedEvents,
252
+ 'mediaElement': mediaPlayer,
253
+ 'textTrackData': mediaPlayer.textTracks[0],
254
+ }
255
+
256
+ // Add click listeners to words
257
+ if (ttClickable) {
258
+ for (const word of document.querySelectorAll('.tt-word')) {
259
+ word.addEventListener('click', handleWordClick);
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+
266
+ // Unlink transcript from previous VTT
267
+ function unlinkTranscript(transcript) {
268
+ clearHighlightedWords(transcript);
269
+
270
+ var ttLinkedElements = transcript.querySelectorAll('[data-tt-word]');
271
+ for (const element of ttLinkedElements) {
272
+ element.dataset.ttWord = '';
273
+ element.dataset.ttPhrase = '';
274
+ element.dataset.ttBlock = '';
275
+ }
276
+
277
+ var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
278
+ if (mediaUrl) {
279
+ delete ttLinkedDataByMediaUrl[mediaUrl]
280
+ transcript.dataset.ttCurrentMediaUrl = '';
281
+ }
282
+
283
+ for (const word of document.querySelectorAll('.tt-word')) {
284
+ word.removeEventListener('click', handleWordClick);
285
+ }
286
+ }
287
+
288
+
289
+ // Convert WebVTT into a wordTimings list
290
+ function parseVttToWordTimings(vttContent) {
291
+ var wordTimings = [];
292
+
293
+ // Split WebVTT at each double line break
294
+ var vttSegments = vttContent.split(/\r?\n\r?\n/);
295
+ if (vttSegments.length == 0 || !vttSegments[0].startsWith('WEBVTT')) {
296
+ console.error('Error: Invalid VTT file. See https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API');
297
+ return;
298
+ }
299
+
300
+ // Loop through each segment of the WebVTT
301
+ // WebVTT documentation: https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API
302
+ var cueWordCounter = 0;
303
+ var cuePhraseCounter = 0;
304
+ var cueBlockCounter = 0;
305
+ for (let s = 0; s < vttSegments.length; s++) {
306
+ var segment = vttSegments[s];
307
+
308
+ // Skip segment if it's not a cue
309
+ if (segment.startsWith('WEBVTT') || /^STYLE\s/.test(segment) || /^NOTE\s/.test(segment)) continue;
310
+
311
+ // Convert VTT timestamps to seconds
312
+ function convertVttTimestampToSeconds(vttTimestamp) {
313
+ var parts = vttTimestamp.trim().split(':');
314
+ var seconds = 0;
315
+ if (parts.length == 3) { // 00:02:01.003
316
+ seconds = (parseFloat(parts[0]) * 60 * 60) + (parseFloat(parts[1]) * 60) + parseFloat(parts[2])
317
+ } else { // 02:01.003
318
+ seconds = (parseFloat(parts[0]) * 60) + parseFloat(parts[1])
319
+ }
320
+ return seconds;
321
+ }
322
+
323
+ // Parse cue
324
+ var cueIdentifier = cuePayload = '';
325
+ var cueStartSeconds = cueEndSeconds = null;
326
+ var segmentLines = segment.split(/\r?\n/);
327
+ for (const line of segmentLines) {
328
+ if (line.includes('-->')) {
329
+ // Cue timings (single line)
330
+ cueStartSeconds = wordStartSeconds = convertVttTimestampToSeconds(line.split('-->')[0].trim());
331
+ cueEndSeconds = convertVttTimestampToSeconds(line.split('-->')[1].trim());
332
+ } else if (cueStartSeconds == null) {
333
+ // Cue identifier (optional)
334
+ cueIdentifier += line;
335
+ } else {
336
+ // Cue payload (may be multiple lines)
337
+ var adjustedLine = line.replace(/(<\d*:?\d+:\d+\.\d+>)\s/g, ' $1').replace(/(<\d*:?\d+:\d+\.\d+>)(<\d*:?\d+:\d+\.\d+>)/g, '$1 $2');
338
+ cuePayload += adjustedLine;
339
+ var cueWords = adjustedLine.split(/\s/)
340
+ for (let word of cueWords) {
341
+ var matches = Array.from(word.matchAll(/(^<\d*:?\d+:\d+\.\d+>)?(.+$)/g));
342
+ if (matches.length == 1 && matches[0][1] != null) {
343
+ match = matches[0];
344
+ wordStartSeconds = convertVttTimestampToSeconds(match[1].replace('<', '').replace('>', ''));
345
+ word = match[2];
346
+ }
347
+
348
+ // Push word to wordTimings list
349
+ if (word) {
350
+ wordTimings.push({
351
+ 'text': word,
352
+ 'startSeconds': wordStartSeconds,
353
+ 'endSeconds': cueEndSeconds,
354
+ 'wordIndex': cueWordCounter,
355
+ 'phraseIndex': cuePhraseCounter,
356
+ 'blockIndex': cueBlockCounter,
357
+ })
358
+ cueWordCounter += 1;
359
+ }
360
+ }
361
+ cuePhraseCounter += 1;
362
+ }
363
+ }
364
+ cueBlockCounter += 1;
365
+ }
366
+
367
+ return wordTimings;
368
+ }
369
+
370
+
371
+ // Respond to timeupdate event (progress as the audio or video is playing)
372
+ var ttCurrentTranscript = null;
373
+ var ttPreviousEvent = null;
374
+ var ttCurrentEvent = null;
375
+ var ttNextEvent = null;
376
+ function ttTimeUpdate(e) {
377
+ // If the current player isn't active or doesn't have data, return
378
+ if (!ttActivePlayer || e.currentTarget != ttActivePlayer || !(ttActivePlayer.dataset.ttLinkedMediaUrl in ttLinkedDataByMediaUrl)) return;
379
+
380
+ var adjustedCurrentTime = ttActivePlayer.currentTime + (ttTimeOffset * -1);
381
+ var ttData = ttLinkedDataByMediaUrl[ttActivePlayer.dataset.ttLinkedMediaUrl];
382
+
383
+ // Make sure the correct transcript is selected
384
+ if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
385
+ ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
386
+ }
387
+
388
+ // If before the first event, after the last event, or within the range of the current event, return
389
+ if (ttCurrentEvent && (ttCurrentEvent.seconds < ttData.timedEvents[0].seconds || ttCurrentEvent.seconds > ttData.timedEvents[ttData.timedEvents.length-1].seconds)) return;
390
+ if (ttCurrentEvent && ttNextEvent && ttCurrentEvent.seconds <= adjustedCurrentTime && ttNextEvent.seconds > adjustedCurrentTime) return;
391
+
392
+ // Clear words that were highlighted from the previous event
393
+ clearHighlightedWords(ttCurrentTranscript);
394
+
395
+ // Add highlights for the current event
396
+ for (let t = 0; t < ttData.timedEvents.length; t++) {
397
+ if (ttData.timedEvents[t].seconds <= adjustedCurrentTime && (!ttData.timedEvents[t+1] || adjustedCurrentTime < ttData.timedEvents[t+1]?.seconds)) {
398
+
399
+ ttPreviousEvent = ttData.timedEvents[t-1] || null;
400
+ ttCurrentEvent = ttData.timedEvents[t];
401
+ ttNextEvent = ttData.timedEvents[t+1] || null;
402
+
403
+ // Mark blocks
404
+ if (ttCurrentEvent.blockIndex != null) {
405
+ var blockElements = ttCurrentTranscript.querySelectorAll(`[data-tt-block="${ttCurrentEvent.blockIndex}"]`);
406
+ for (let b = 0; b < blockElements.length; b++) blockElements[b].classList.add((b==0 && !blockElements[b].classList.contains('tt-word')) ? 'tt-current-block-container' : 'tt-current-block');
407
+ }
408
+
409
+ // Mark phrases
410
+ if (ttCurrentEvent.phraseIndex != null) {
411
+ var phraseElements = ttCurrentTranscript.querySelectorAll(`[data-tt-phrase="${ttCurrentEvent.phraseIndex}"]`);
412
+ for (let p = 0; p < phraseElements.length; p++) phraseElements[p].classList.add((p==0 && !phraseElements[p].classList.contains('tt-word')) ? 'tt-current-phrase-container' : 'tt-current-phrase');
413
+ }
414
+
415
+ // Mark words
416
+ if (ttCurrentEvent.currentWordIndexes.length > 0) {
417
+ for (const wordIndex of ttCurrentEvent.currentWordIndexes) {
418
+ var wordElements = ttCurrentTranscript.querySelectorAll(`[data-tt-word="${wordIndex}"]`);
419
+ for (const wordElement of wordElements) wordElement.classList.add('tt-current-word');
420
+ }
421
+ for (const wordElement of ttCurrentTranscript.getElementsByClassName('tt-word')) {
422
+ if (wordElement.classList.contains('tt-current-word')) break;
423
+ wordElement.classList.add('tt-previous-word');
424
+ }
425
+ }
426
+
427
+ // Auto-scroll to the highlighted text
428
+ if (ttAutoScroll) {
429
+ var scrollOptions = { behavior: 'smooth', block: 'start', inline: 'nearest' }
430
+ if (ttAutoScroll == 'block' && ttPreviousEvent?.blockIndex != ttCurrentEvent.blockIndex) {
431
+ document.querySelector('.tt-current-block-container').scrollIntoView(scrollOptions);
432
+ } else if (ttAutoScroll == 'phrase' && ttPreviousEvent?.phraseIndex != ttCurrentEvent.phraseIndex) {
433
+ document.querySelector('.tt-current-phrase-container').scrollIntoView(scrollOptions);
434
+ } else if (ttAutoScroll == 'word') {
435
+ document.querySelector('.tt-current-word').scrollIntoView(scrollOptions);
436
+ }
437
+ }
438
+
439
+ break;
440
+ }
441
+ }
442
+ }
443
+
444
+
445
+ // Clear highlighted words in transcript
446
+ function clearHighlightedWords(transcript) {
447
+ if (!transcript) return;
448
+ var ttHighlightedElements = transcript.querySelectorAll('[class*="tt-current"], [class*="tt-previous"]');
449
+ for (const element of ttHighlightedElements) {
450
+ element.classList.remove('tt-current-block', 'tt-current-block-container', 'tt-current-phrase', 'tt-current-phrase-container', 'tt-current-word', 'tt-previous-word');
451
+ }
452
+ }
453
+
454
+
455
+ // Handle when a word in the transcript with an event listener is clicked
456
+ function handleWordClick(e) {
457
+ var wordElement = e.currentTarget;
458
+ var wordIndex = wordElement.dataset.ttWord;
459
+ var transcript = wordElement.closest('.tt-transcript');
460
+ var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
461
+ var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds
462
+ ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds;
463
+ }