forked from lukevp/pluralsight-transcript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcript.js
120 lines (115 loc) · 5.08 KB
/
transcript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var title = "";
var currentTab = -1;
var currentModuleAndClip = "";
var transcriptData = undefined;
var rendered = false;
/* From: http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript & heavily modified */
function parseQuery(url) {
var query = {};
var result = url.split('?');
var querystring = url;
if (result.length > 1) {
querystring = result[result.length - 1].split('&');
}
for (var i = 0; i < querystring.length; i++) {
var components = querystring[i].split('=');
query[decodeURIComponent(components[0])] = decodeURIComponent(components[1] || '');
}
return query;
}
function updateTitle(tab) {
currentTab = tab.id;
var urlParams = parseQuery(tab.url);
title = urlParams["course"];
document.title = "Transcript for course: " + title;
}
browser.tabs.getCurrent().then(updateTitle, undefined);
function renderTranscript() {
var text = "";
for (var moduleindex = 0; moduleindex < transcriptData.modules.length; moduleindex++) {
var module = transcriptData.modules[moduleindex];
var moduleroot = "module" + moduleindex
text += "<div id='" + moduleroot + "' class='module'><h1>" + module.title + "</h1>";
for (var clipindex = 0; clipindex < module.clips.length; clipindex++) {
var clip = module.clips[clipindex];
var cliproot = moduleroot + "clip" + clipindex;
text += "<div id='" + cliproot + "' class='clip'><h2>" + clip.title + "</h2><div class='clip-body'><p>";
var sentenceCount = 0;
for (var segmentindex = 0; segmentindex < clip.segments.length; segmentindex++) {
var segment = clip.segments[segmentindex];
/* Count the # of sentences & add a break every 5 sentences. */
var newText = "";
var endlocation = 0;
var foundIndex = 0;
while (endlocation >= 0) {
var endlocation = segment.text.search(/\.(\s|$)/);
if (endlocation != -1) {
newText += segment.text.substring(0, endlocation + 1);
sentenceCount += 1;
if (sentenceCount >= 5) {
newText += "</span></p><p><span id='" + cliproot + "segment" + segmentindex + "-1' class='segment'>";
sentenceCount = 0;
}
segment.text = segment.text.substring(endlocation + 1);
}
else {
newText += segment.text;
}
}
text += "<span id='" + cliproot + "segment" + segmentindex + "' class='segment'>" + newText + "</span> ";
}
text += "</p></div></div>";
}
text += "</div>";
}
document.body.innerHTML = text;
rendered = true;
}
function updateTranscript(module, clip, time) {
if (!rendered) {
return;
}
var selected = document.querySelectorAll('.selected');
for (var i = 0; i < selected.length; i++) {
selected[i].classList.remove('selected');
}
document.getElementById('module' + module).classList.add('selected');
document.getElementById('module' + module + 'clip' + clip).classList.add('selected');
// If clip/module has changed, scroll to new clip.
var newModuleAndClip = 'm' + module + 'c' + clip;
if (currentModuleAndClip != newModuleAndClip) {
window.scrollTo(0, document.getElementById('module' + module + 'clip' + clip).offsetTop);
currentModuleAndClip = newModuleAndClip;
}
// Find the clip we're on and get our list of segments.
var segments = transcriptData.modules[module].clips[clip].segments;
// Select the active segment. Default to the last segment.
var activeSegment = -1;
for (var i = segments.length - 1; i >= 0; i--) {
if (time >= segments[i].displayTime) {
activeSegment = i;
break;
}
}
var lowestBeforeScrolling = (window.pageYOffset + window.innerHeight) - 150;
var endOfScreen = (window.pageYOffset + window.innerHeight);
var segmentTop = document.getElementById('module' + module + 'clip' + clip + 'segment' + (activeSegment)).offsetTop;
if (lowestBeforeScrolling < segmentTop && segmentTop < endOfScreen) {
window.scrollTo(0, segmentTop - window.innerHeight + 155);
}
document.getElementById('module' + module + 'clip' + clip + 'segment' + (activeSegment)).classList.add('selected');
var subSegment = document.getElementById('module' + module + 'clip' + clip + 'segment' + (activeSegment) + "-1");
if (subSegment !== null) {
subSegment.classList.add('selected');
}
}
function handleMessage(request, sender, sendResponse) {
if (request.course == title) {
if (transcriptData == undefined && request.transcript != undefined) {
transcriptData = request.transcript;
renderTranscript();
}
updateTranscript(request.module, request.clip, request.time);
}
}
browser.runtime.onMessage.addListener(handleMessage);