-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
78 lines (74 loc) · 2.99 KB
/
background.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
// Production Regex
var playerRegex = /^https?\:\/\/app\.pluralsight\.com\/player\?.*course=([^&]*)(&|$)/ig;
// Local Mock Regex (IIS can't easily serve static files with no extension).
//var playerRegex = /^https?\:\/\/app\.pluralsight\.com\/player.html\?.*course=([^&]*)(&|$)/ig;
/*
Define callback function that will lookup the active course popup in our list and
Update the DOM to highlight the current line of the transcript.
Also define a second callback that will call updateTranscriptTIme
*/
function updateTranscriptTime(m) {
if (m.source == "content") {
console.log("In background script, received message from content script");
console.log("MODULE: " + m.module);
console.log("CLIP: " + m.clip);
console.log("TIME: " + m.time);
}
else if (m.source == "transcript") {
console.log("In background script, received message from transcript");
console.log("TabID: " + m.tabid);
console.log("Title: " + m.title);
}
}
function onCreated(windowInfo, tab, active_course) {
console.log(`Created window: ${windowInfo.id}`);
}
function onError(error) {
console.log(`Error: ${error}`);
}
/* Define callback function for onclick of Page Action that will launch transcript
popup and inject script into player page to update us with the current player time
*/
function showTranscript(tab) {
active_course = playerRegex.exec(tab.url)[1];
console.log("Show Transcript");
console.log("Tab: " + tab);
console.log("Course: " + active_course);
// Returns 404 if can't find course.
// What if can't find transcript but course exists?
var transcriptURL = "transcript.html";
transcriptURL += "?course=" + active_course;
var creating = browser.windows.create(
{
type: "popup",
url: transcriptURL
});
creating.then(function (windowInfo) { onCreated(windowInfo, tab, active_course); }, onError);
}
/* Register an event handler for the pageClick event to open a pop-up window
with the active course's transcript. */
browser.pageAction.onClicked.addListener(showTranscript);
/* Register event handler to new tab creation to add the page action button
whenever the user is at a pluralsight player page. */
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (tab.url.match(playerRegex)) {
// This is a current bug in polyfill and the pageAction.show wrapper does not work properly in chrome
// so we need to manually check for presence of chrome
if (chrome !== undefined) {
chrome.pageAction.show(tab.id);
}
else {
browser.pageAction.show(tab.id);
}
}
else {
// This is a current bug in polyfill and the pageAction.show wrapper does not work properly in chrome
// so we need to manually check for presence of chrome
if (chrome !== undefined) {
chrome.pageAction.hide(tab.id);
}
else {
browser.pageAction.hide(tab.id);
}
}
});