|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ttIsInitialized = false; |
|
var ttTranscripts; |
|
var ttMediaPlayers; |
|
var ttActivePlayer; |
|
var ttLinkedDataByMediaUrl = {}; |
|
|
|
|
|
var ttBlockSelector = null; |
|
var ttPhraseSelector = null; |
|
var ttAlignmentFuzziness = 0; |
|
var ttTimeOffset = 0; |
|
var ttAutoScroll = null; |
|
var ttClickable = false; |
|
|
|
|
|
|
|
function loadTranscriptTracer(options=null) { |
|
if (document.readyState == 'loading') { |
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
loadTranscriptTracer(options); |
|
}); |
|
return; |
|
} |
|
|
|
|
|
if (options) { |
|
if ('blockSelector' in options) ttBlockSelector = options.blockSelector; |
|
if ('phraseSelector' in options) ttPhraseSelector = options.phraseSelector; |
|
if ('alignmentFuzziness' in options) ttAlignmentFuzziness = options.alignmentFuzziness; |
|
if ('timeOffset' in options) ttTimeOffset = options.timeOffset; |
|
if ('autoScroll' in options) ttAutoScroll = options.autoScroll; |
|
if ('clickable' in options) ttClickable = options.clickable; |
|
} |
|
|
|
|
|
if (ttIsInitialized) { |
|
if (ttTranscripts) for (const transcript of ttTranscripts) { |
|
unlinkTranscript(transcript); |
|
transcript.dataset.ttTranscript = ''; |
|
} |
|
ttTranscripts = null; |
|
ttMediaPlayers = null; |
|
ttActivePlayer = null; |
|
ttLinkedDataByMediaUrl = {}; |
|
document.querySelectorAll('.tt-word, .tt-whitespace').forEach(element => { |
|
element.outerHTML = element.innerHTML; |
|
}); |
|
} |
|
|
|
|
|
ttIsInitialized = true; |
|
ttTranscripts = document.getElementsByClassName('tt-transcript'); |
|
ttMediaPlayers = document.querySelectorAll('audio, video'); |
|
|
|
|
|
for (let t = 0; t < ttTranscripts.length; t++) { |
|
var transcript = ttTranscripts[t]; |
|
|
|
if (!transcript.dataset.ttMediaUrls) continue; |
|
|
|
transcript.dataset.ttTranscript = t; |
|
|
|
|
|
|
|
var iter = document.createNodeIterator(transcript, NodeFilter.SHOW_TEXT); |
|
var textNode; |
|
while (textNode = iter.nextNode()) { |
|
const text = textNode.textContent; |
|
if (text.replace(/\s/g, '').length != 0) { |
|
var spannedText = '<span class="tt-word">' + text.replace(/(\s+)/g, '</span><span class="tt-whitespace">$1</span><span class="tt-word">') + '</span>'; |
|
spannedText = spannedText.replaceAll('<span class="tt-word"></span>', ''); |
|
|
|
|
|
const template = document.createElement('template'); |
|
template.innerHTML = spannedText; |
|
textNode.parentNode.insertBefore(template.content, textNode); |
|
textNode.parentNode.removeChild(textNode); |
|
} |
|
} |
|
} |
|
|
|
for (const mediaPlayer of ttMediaPlayers) { |
|
|
|
linkTranscripts(mediaPlayer); |
|
|
|
|
|
mediaPlayer.addEventListener('play', function(e) { |
|
if (ttActivePlayer != e.currentTarget) { |
|
if (ttActivePlayer) { |
|
ttActivePlayer.pause(); |
|
ttActivePlayer.removeEventListener('timeupdate', ttTimeUpdate); |
|
} |
|
ttActivePlayer = e.currentTarget; |
|
if (ttCurrentTranscript) clearHighlightedWords(ttCurrentTranscript); |
|
if (ttCurrentEvent) ttCurrentEvent = null; |
|
} |
|
ttActivePlayer.addEventListener('timeupdate', ttTimeUpdate); |
|
var currentTranscript = ttTranscripts[ttLinkedDataByMediaUrl[ttActivePlayer.dataset.ttLinkedMediaUrl].transcriptIndex]; |
|
currentTranscript.dataset.ttCurrentMediaUrl = ttActivePlayer.dataset.ttLinkedMediaUrl; |
|
}); |
|
mediaPlayer.addEventListener('ended', function(e) { |
|
if (ttCurrentTranscript) clearHighlightedWords(ttCurrentTranscript); |
|
if (ttCurrentEvent) ttCurrentEvent = null; |
|
}); |
|
} |
|
} |
|
|
|
|
|
|
|
function linkTranscripts(mediaPlayer) { |
|
var trackElement = mediaPlayer.querySelector('track[kind="metadata"]') |
|
|
|
var mediaPlayerSourceUrls = []; |
|
var mediaPlayerSrc = mediaPlayer.getAttribute('src'); |
|
var mediaPlayerSourceElements = mediaPlayer.querySelectorAll('source'); |
|
if (mediaPlayerSrc) mediaPlayerSourceUrls.push(mediaPlayerSrc); |
|
if (mediaPlayerSourceElements) for (const s of mediaPlayerSourceElements) mediaPlayerSourceUrls.push(s.src); |
|
|
|
|
|
if (!trackElement || !trackElement.getAttribute('src') || mediaPlayerSourceUrls.length == 0) return; |
|
|
|
|
|
for (const transcript of ttTranscripts) { |
|
for (const mediaUrl of mediaPlayerSourceUrls) { |
|
if (transcript.dataset.ttMediaUrls.includes(mediaUrl)) { |
|
mediaPlayer.dataset.ttLinkedMediaUrl = mediaUrl; |
|
unlinkTranscript(transcript); |
|
fetch(trackElement.src) |
|
.then(r => r.text()) |
|
.then(vttContent => linkTranscript(mediaPlayer, vttContent, transcript)); |
|
break; |
|
} |
|
} |
|
} |
|
|
|
function linkTranscript(mediaPlayer, vttContent, transcript) { |
|
var wordTimings = parseVttToWordTimings(vttContent); |
|
transcript.dataset.ttCurrentMediaUrl = mediaPlayer.dataset.ttLinkedMediaUrl; |
|
|
|
function normalizedWord(word) { |
|
|
|
return word.toLowerCase().normalize('NFD').replace(/[^\p{L}\p{N}]/gu, ''); |
|
} |
|
|
|
|
|
var blockContainers = ttBlockSelector ? transcript.querySelectorAll(ttBlockSelector) : []; |
|
for (let c = 0; c < blockContainers.length; c++) blockContainers[c].dataset.ttBlock = c; |
|
var phraseContainers = ttPhraseSelector ? transcript.querySelectorAll(ttPhraseSelector) : []; |
|
for (let c = 0; c < phraseContainers.length; c++) phraseContainers[c].dataset.ttPhrase = c; |
|
|
|
|
|
var timedEvents = []; |
|
var wordTimingsIndex = 0; |
|
var wordSpans = transcript.getElementsByClassName('tt-word'); |
|
for (let s = 0; s < wordSpans.length; s++) { |
|
var span = wordSpans[s]; |
|
|
|
|
|
var initialWordTimingsIndex = wordTimingsIndex; |
|
var maxFuzzyWordTimingsIndex = Math.min(wordTimingsIndex + ttAlignmentFuzziness, wordTimings.length - 1); |
|
while (normalizedWord(span.innerText) != normalizedWord(wordTimings[wordTimingsIndex].text) && wordTimingsIndex <= maxFuzzyWordTimingsIndex) { |
|
wordTimingsIndex += 1; |
|
} |
|
if (normalizedWord(span.innerText) != normalizedWord(wordTimings[wordTimingsIndex].text)) { |
|
|
|
wordTimingsIndex = initialWordTimingsIndex; |
|
continue; |
|
} |
|
|
|
|
|
blockIndex = ttBlockSelector ? (span.closest(ttBlockSelector)?.dataset?.ttBlock ?? null) : wordTimings[wordTimingsIndex].blockIndex; |
|
phraseIndex = ttPhraseSelector ? (span.closest(ttPhraseSelector)?.dataset?.ttPhrase ?? null) : wordTimings[wordTimingsIndex].phraseIndex; |
|
wordIndex = wordTimings[wordTimingsIndex].wordIndex; |
|
|
|
|
|
span.dataset.ttBlock = blockIndex; |
|
span.dataset.ttPhrase = phraseIndex; |
|
span.dataset.ttWord = wordIndex; |
|
|
|
|
|
if (timedEvents.length != 0 && wordTimings[wordTimingsIndex].startSeconds == timedEvents[timedEvents.length-1].seconds) { |
|
timedEvents[timedEvents.length-1].currentWordIndexes.push(wordIndex); |
|
} else { |
|
timedEvents.push({ |
|
'seconds': wordTimings[wordTimingsIndex].startSeconds, |
|
'currentWordIndexes': [wordIndex], |
|
'phraseIndex': phraseIndex, |
|
'blockIndex': blockIndex, |
|
}); |
|
} |
|
|
|
wordTimingsIndex += 1; |
|
} |
|
|
|
|
|
function findRelevantParent(startingElement, endingElement, childSelector, relevantChildSelector) { |
|
var currentElement = startingElement; |
|
while (currentElement && currentElement != endingElement) { |
|
var currentElement = currentElement.parentElement; |
|
var children = currentElement.querySelectorAll(childSelector); |
|
var relevantChildren = document.querySelectorAll(relevantChildSelector); |
|
if (children.length == relevantChildren.length) { |
|
|
|
return currentElement; |
|
} else if (children.length > relevantChildren.length) { |
|
|
|
break; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
|
|
if (!ttBlockSelector) { |
|
var count = wordTimings[wordTimings.length-1].blockIndex + 1; |
|
for (let c = 0; c < count; c++) { |
|
var startingElement = document.querySelector(`[data-tt-block="${c}"]`); |
|
var blockContainer = findRelevantParent(startingElement, transcript, '[data-tt-word]', `[data-tt-word][data-tt-block="${c}"]`); |
|
if (blockContainer) blockContainer.dataset.ttBlock = c; |
|
} |
|
} |
|
if (!ttPhraseSelector) { |
|
var count = wordTimings[wordTimings.length-1].phraseIndex + 1; |
|
for (let c = 0; c < count; c++) { |
|
var startingElement = document.querySelector(`[data-tt-phrase="${c}"]`); |
|
var phraseContainer = findRelevantParent(startingElement, transcript, '[data-tt-word]', `[data-tt-word][data-tt-phrase="${c}"]`); |
|
if (phraseContainer) phraseContainer.dataset.ttPhrase = c; |
|
} |
|
} |
|
|
|
|
|
timedEvents = timedEvents.sort(function(a, b) { |
|
return a.seconds - b.seconds; |
|
}) |
|
|
|
|
|
var transcriptIndex = parseInt(transcript.dataset.ttTranscript); |
|
ttLinkedDataByMediaUrl[mediaPlayer.dataset.ttLinkedMediaUrl] = { |
|
'transcriptIndex': transcriptIndex, |
|
'wordTimings': wordTimings, |
|
'timedEvents': timedEvents, |
|
'mediaElement': mediaPlayer, |
|
'textTrackData': mediaPlayer.textTracks[0], |
|
} |
|
|
|
|
|
if (ttClickable) { |
|
for (const word of document.querySelectorAll('.tt-word')) { |
|
word.addEventListener('click', handleWordClick); |
|
} |
|
|
|
for (const word of document.querySelectorAll('.tt-word')) { |
|
word.addEventListener('dblclick', handleWordDBClick); |
|
} |
|
|
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
function unlinkTranscript(transcript) { |
|
clearHighlightedWords(transcript); |
|
|
|
var ttLinkedElements = transcript.querySelectorAll('[data-tt-word]'); |
|
for (const element of ttLinkedElements) { |
|
element.dataset.ttWord = ''; |
|
element.dataset.ttPhrase = ''; |
|
element.dataset.ttBlock = ''; |
|
} |
|
|
|
var mediaUrl = transcript.dataset.ttCurrentMediaUrl; |
|
if (mediaUrl) { |
|
delete ttLinkedDataByMediaUrl[mediaUrl] |
|
transcript.dataset.ttCurrentMediaUrl = ''; |
|
} |
|
|
|
for (const word of document.querySelectorAll('.tt-word')) { |
|
word.removeEventListener('click', handleWordClick); |
|
} |
|
|
|
for (const word of document.querySelectorAll('.tt-word')) { |
|
word.removeEventListener('dblclick', handleWordDBClick); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function parseVttToWordTimings(vttContent) { |
|
var wordTimings = []; |
|
|
|
|
|
var vttSegments = vttContent.split(/\r?\n\r?\n/); |
|
if (vttSegments.length == 0 || !vttSegments[0].startsWith('WEBVTT')) { |
|
console.error('Error: Invalid VTT file. See https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API'); |
|
return; |
|
} |
|
|
|
|
|
|
|
var cueWordCounter = 0; |
|
var cuePhraseCounter = 0; |
|
var cueBlockCounter = 0; |
|
for (let s = 0; s < vttSegments.length; s++) { |
|
var segment = vttSegments[s]; |
|
|
|
|
|
if (segment.startsWith('WEBVTT') || /^STYLE\s/.test(segment) || /^NOTE\s/.test(segment)) continue; |
|
|
|
|
|
function convertVttTimestampToSeconds(vttTimestamp) { |
|
var parts = vttTimestamp.trim().split(':'); |
|
var seconds = 0; |
|
if (parts.length == 3) { |
|
seconds = (parseFloat(parts[0]) * 60 * 60) + (parseFloat(parts[1]) * 60) + parseFloat(parts[2]) |
|
} else { |
|
seconds = (parseFloat(parts[0]) * 60) + parseFloat(parts[1]) |
|
} |
|
return seconds; |
|
} |
|
|
|
|
|
var cueIdentifier = cuePayload = ''; |
|
var cueStartSeconds = cueEndSeconds = null; |
|
var segmentLines = segment.split(/\r?\n/); |
|
for (const line of segmentLines) { |
|
if (line.includes('-->')) { |
|
|
|
cueStartSeconds = wordStartSeconds = convertVttTimestampToSeconds(line.split('-->')[0].trim()); |
|
cueEndSeconds = convertVttTimestampToSeconds(line.split('-->')[1].trim()); |
|
} else if (cueStartSeconds == null) { |
|
|
|
cueIdentifier += line; |
|
} else { |
|
|
|
var adjustedLine = line.replace(/(<\d*:?\d+:\d+\.\d+>)\s/g, ' $1').replace(/(<\d*:?\d+:\d+\.\d+>)(<\d*:?\d+:\d+\.\d+>)/g, '$1 $2'); |
|
cuePayload += adjustedLine; |
|
var cueWords = adjustedLine.split(/\s/) |
|
for (let word of cueWords) { |
|
var matches = Array.from(word.matchAll(/(^<\d*:?\d+:\d+\.\d+>)?(.+$)/g)); |
|
if (matches.length == 1 && matches[0][1] != null) { |
|
match = matches[0]; |
|
wordStartSeconds = convertVttTimestampToSeconds(match[1].replace('<', '').replace('>', '')); |
|
word = match[2]; |
|
} |
|
|
|
|
|
if (word) { |
|
wordTimings.push({ |
|
'text': word, |
|
'startSeconds': wordStartSeconds, |
|
'endSeconds': cueEndSeconds, |
|
'wordIndex': cueWordCounter, |
|
'phraseIndex': cuePhraseCounter, |
|
'blockIndex': cueBlockCounter, |
|
}) |
|
cueWordCounter += 1; |
|
} |
|
} |
|
cuePhraseCounter += 1; |
|
} |
|
} |
|
cueBlockCounter += 1; |
|
} |
|
|
|
return wordTimings; |
|
} |
|
|
|
|
|
|
|
var ttCurrentTranscript = null; |
|
var ttPreviousEvent = null; |
|
var ttCurrentEvent = null; |
|
var ttNextEvent = null; |
|
function ttTimeUpdate(e) { |
|
|
|
if (!ttActivePlayer || e.currentTarget != ttActivePlayer || !(ttActivePlayer.dataset.ttLinkedMediaUrl in ttLinkedDataByMediaUrl)) return; |
|
|
|
var adjustedCurrentTime = ttActivePlayer.currentTime + (ttTimeOffset * -1); |
|
var ttData = ttLinkedDataByMediaUrl[ttActivePlayer.dataset.ttLinkedMediaUrl]; |
|
|
|
|
|
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) { |
|
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`); |
|
} |
|
|
|
|
|
if (ttCurrentEvent && (ttCurrentEvent.seconds < ttData.timedEvents[0].seconds || ttCurrentEvent.seconds > ttData.timedEvents[ttData.timedEvents.length-1].seconds)) return; |
|
if (ttCurrentEvent && ttNextEvent && ttCurrentEvent.seconds <= adjustedCurrentTime && ttNextEvent.seconds > adjustedCurrentTime) return; |
|
|
|
|
|
clearHighlightedWords(ttCurrentTranscript); |
|
|
|
|
|
for (let t = 0; t < ttData.timedEvents.length; t++) { |
|
if (ttData.timedEvents[t].seconds <= adjustedCurrentTime && (!ttData.timedEvents[t+1] || adjustedCurrentTime < ttData.timedEvents[t+1]?.seconds)) { |
|
|
|
ttPreviousEvent = ttData.timedEvents[t-1] || null; |
|
ttCurrentEvent = ttData.timedEvents[t]; |
|
ttNextEvent = ttData.timedEvents[t+1] || null; |
|
|
|
|
|
if (ttCurrentEvent.blockIndex != null) { |
|
var blockElements = ttCurrentTranscript.querySelectorAll(`[data-tt-block="${ttCurrentEvent.blockIndex}"]`); |
|
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'); |
|
} |
|
|
|
|
|
if (ttCurrentEvent.phraseIndex != null) { |
|
var phraseElements = ttCurrentTranscript.querySelectorAll(`[data-tt-phrase="${ttCurrentEvent.phraseIndex}"]`); |
|
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'); |
|
} |
|
|
|
|
|
if (ttCurrentEvent.currentWordIndexes.length > 0) { |
|
for (const wordIndex of ttCurrentEvent.currentWordIndexes) { |
|
var wordElements = ttCurrentTranscript.querySelectorAll(`[data-tt-word="${wordIndex}"]`); |
|
for (const wordElement of wordElements) wordElement.classList.add('tt-current-word'); |
|
} |
|
for (const wordElement of ttCurrentTranscript.getElementsByClassName('tt-word')) { |
|
if (wordElement.classList.contains('tt-current-word')) break; |
|
wordElement.classList.add('tt-previous-word'); |
|
} |
|
} |
|
|
|
|
|
if (ttAutoScroll && !ttActivePlayer.paused() && ttActivePlayer.duration != 0) { |
|
var scrollOptions = { behavior: 'smooth', block: 'center', inline: 'nearest' } |
|
if (ttAutoScroll == 'block' && ttPreviousEvent?.blockIndex != ttCurrentEvent.blockIndex) { |
|
document.querySelector('.tt-current-block-container').scrollIntoView(scrollOptions); |
|
} else if (ttAutoScroll == 'phrase' && ttPreviousEvent?.phraseIndex != ttCurrentEvent.phraseIndex) { |
|
document.querySelector('.tt-current-phrase-container').scrollIntoView(scrollOptions); |
|
} else if (ttAutoScroll == 'word') { |
|
document.querySelector('.tt-current-word').scrollIntoView(scrollOptions); |
|
} |
|
} |
|
|
|
break; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
function clearHighlightedWords(transcript) { |
|
if (!transcript) return; |
|
var ttHighlightedElements = transcript.querySelectorAll('[class*="tt-current"], [class*="tt-previous"]'); |
|
for (const element of ttHighlightedElements) { |
|
element.classList.remove('tt-current-block', 'tt-current-block-container', 'tt-current-phrase', 'tt-current-phrase-container', 'tt-current-word', 'tt-previous-word'); |
|
} |
|
} |
|
|
|
|
|
|
|
function handleWordClick(e) { |
|
var wordElement = e.currentTarget; |
|
var wordIndex = wordElement.dataset.ttWord; |
|
var transcript = wordElement.closest('.tt-transcript'); |
|
var mediaUrl = transcript.dataset.ttCurrentMediaUrl; |
|
var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds |
|
|
|
ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds; |
|
|
|
var ttData = ttLinkedDataByMediaUrl[mediaUrl]; |
|
|
|
|
|
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) { |
|
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`); |
|
} |
|
|
|
|
|
clearHighlightedWords(ttCurrentTranscript); |
|
|
|
|
|
wordElement.classList.add('tt-current-word'); |
|
|
|
for (const wordElement of ttCurrentTranscript.getElementsByClassName('tt-word')) { |
|
if (wordElement.classList.contains('tt-current-word')) break; |
|
wordElement.classList.add('tt-previous-word'); |
|
} |
|
|
|
|
|
ttActivePlayer.pause() |
|
|
|
} |
|
|
|
|
|
function handleWordDBClick(e) { |
|
var wordElement = e.currentTarget; |
|
var wordIndex = wordElement.dataset.ttWord; |
|
var transcript = wordElement.closest('.tt-transcript'); |
|
var mediaUrl = transcript.dataset.ttCurrentMediaUrl; |
|
var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds |
|
|
|
ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds; |
|
|
|
var ttData = ttLinkedDataByMediaUrl[mediaUrl]; |
|
|
|
|
|
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) { |
|
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`); |
|
} |
|
|
|
|
|
clearHighlightedWords(ttCurrentTranscript); |
|
|
|
|
|
wordElement.classList.add('tt-current-word'); |
|
|
|
for (const wordElement of ttCurrentTranscript.getElementsByClassName('tt-word')) { |
|
if (wordElement.classList.contains('tt-current-word')) break; |
|
wordElement.classList.add('tt-previous-word'); |
|
} |
|
|
|
|
|
ttActivePlayer.play() |
|
} |
|
|
|
|
|
loadTranscriptTracer({ blockSelector: null, phraseSelector: null, alignmentFuzziness: 0, timeOffset: 0, autoScroll: "word", clickable: true}) |