-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
59 lines (50 loc) · 1.9 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
var tabs = {};
// Setup listener to show/hide the page actions
// https://developer.chrome.com/extensions/pageAction
chrome.webNavigation.onCommitted.addListener(
function (details) {
var matches = /[?&]course=(?:([^&#]*)|&|#|$)/.exec(details.url);
if (matches) {
console.debug('Detected course ' + matches[1]);
tabs[details.tabId] = {
course: matches[1]
};
chrome.tabs.executeScript(details.tabId, {file: "content.js", runAt: "document_end"});
}
chrome.pageAction.hide(details.tabId);
},
{urls: [ '*://app.pluralsight.com/player?*' ]}
);
// Listen to the url used to get the video url
// https://developer.chrome.com/extensions/webRequest#event-onBeforeRequest
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
if (!tabs.hasOwnProperty(details.tabId)) {
console.debug('Tab is not linked to a course');
return;
}
if (tabs[details.tabId].hasOwnProperty('request')) {
return;
}
var postData = String.fromCharCode.apply(null, Array.apply(null, new Uint8Array(details.requestBody.raw[0].bytes)));
var postJson = JSON.parse(postData);
tabs[details.tabId].request = {
method: details.method,
body: postJson
};
chrome.pageAction.show(details.tabId);
},
{urls: [ '*://app.pluralsight.com/player/retrieve-url' ]},
['requestBody']
);
// Listen for the popup to request information
// https://developer.chrome.com/extensions/runtime#event-onMessage
chrome.runtime.onMessage.addListener(function (msg, sender, response) {
if (msg.type === 'initialRequest') {
response(tabs[msg.tabId]);
}
if (msg.type === 'downloadFile') {
console.log('Download ' + msg.url);
// https://developer.chrome.com/extensions/downloads#method-download
}
});