-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
121 lines (112 loc) · 3.82 KB
/
content.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
121
var currentModule = -1;
var currentClip = -1;
var currentTime = -1;
var transcriptData = undefined;
/* Get JSON from pluralsight and parse it into DOM elements.
*/
function renderTranscript(course) {
// Production Transcript URL
var transcriptJSONurl = "https://app.pluralsight.com/learner/courses/" + course + "/transcript";
// Local Mock Transcript URL (IIS can't easily serve static files with no extension).
//var transcriptJSONurl = "http://app.pluralsight.com/learner/courses/" + course + "/transcript.html";
console.log("requesting: " + transcriptJSONurl);
var request = new XMLHttpRequest();
request.open('GET', transcriptJSONurl, true);
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
// Success!
var data = JSON.parse(request.responseText);
transcriptData = data;
} else {
// We reached our target server, but it returned an error
}
};
request.send();
request.onerror = function () {
// There was a connection error of some sort
};
}
urlParams = parseQuery(window.location.href);
var course = urlParams["course"];
renderTranscript(course);
function hasClass(item, className) {
for (var i = 0; i < item.classList.length; i++) {
if (item.classList[i] == className) {
return true;
}
}
return false;
}
var sendDataCalls = 0;
function sendData() {
sendDataCalls += 1;
// Try to send the whole transcript data every couple seconds.
if (sendDataCalls % 8 == 0) {
browser.runtime.sendMessage({
course: course,
module: currentModule,
clip: currentClip,
time: currentTime,
transcript: transcriptData
}).then(function() {} , function() {});
}
else {
browser.runtime.sendMessage({
course: course,
module: currentModule,
clip: currentClip,
time: currentTime
}).then(function() {} , function() {});
}
}
/* 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 updateData() {
// Set up a recursive loop to constantly check to see if any of our watched values
// have changed since the last time, and if so, notify the background script.
window.setTimeout(updateData, 250);
var videoPlayer = document.getElementsByTagName('video')[0];
if (videoPlayer == undefined) {
return;
}
var newTime = videoPlayer.currentTime;
var urlParams = parseQuery(window.location.href);
var nameComponents = urlParams["name"].split('-');
var moduleRegex = /^[mM](\d+)$/g;
var targetModule = "m0";
for (var i = 0; i < nameComponents.length; i++)
{
if (nameComponents[i].match(moduleRegex))
{
targetModule = moduleRegex.exec(nameComponents[i])[1];
break;
}
}
var targetClip = urlParams["clip"];
if (currentModule != targetModule || currentClip != targetClip) {
console.log("Module and clip changed.");
console.log(currentModule);
console.log(targetModule);
console.log(currentClip);
console.log(targetClip);
currentModule = targetModule;
currentClip = targetClip;
}
currentTime = newTime;
sendData();
}
// Invoke updateData immediately upon definition so we don't have any lag time
// from the first time the button is clicked.
updateData();