-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
91 lines (81 loc) · 2.66 KB
/
popup.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
chrome.runtime.onMessage.addListener(
function (request, sender) {
if (request.action == "getSource") {
$('#message').html(getLinks(request.source));
}
}
);
function filterOut(links) {
var goodLinks = [];
if (links) {
links.forEach(function (link) {
if (!/audio/.test(link)) {
goodLinks.push(link);
}
});
}
return goodLinks;
}
function downloadFile(options) {
if (!options.url) {
var blob = new Blob([options.content], { type: "video/mp4" });
options.url = window.URL.createObjectURL(blob);
}
chrome.downloads.download({
url: options.url,
filename: options.filename
})
}
// Download all visible checked links.
function downloadCheckedLinks(options) {
chrome.downloads.download({ url: options.url },
function (id) {
});
window.close();
}
function createDownLoadsHTML(links) {
var html = ["<ul>"];
if (links.length > 0) {
var cnt = 1;
links.forEach(function (link) {
var linkMatch = link.match(/(?:(?:https:\/\/files[\d].lynda.com)(?:\/[\w]+\/[\w]+\/[\w\d]+\/[\w\d_]+\/))([^\s\?]+)/i);
var name = "";
if (linkMatch != undefined || linkMatch != null) {
name = linkMatch[1];
}
html.push("<li>Name: <i>" + name + "</i><br />" + "<button class='downloadBtn' id='" + cnt + "' data-vid='" + link + "'>Video: " + cnt++ + "</button><br /></li>");
});
html.push("</ul>");
} else {
html.push("<li>No Video Links Found.</li></ul>");
}
return html.join("");
}
function getLinks(html) {
var links = html.match(/(?<=progressiveUrl.:.)(https?:\/\/[^\s\"]+)/mgi);
var finalLinks = filterOut(links);
var finalHTML = createDownLoadsHTML(finalLinks);
return finalHTML;
}
function eventListeners() {
$(document.body).on('click', '.downloadBtn', function (e) {
downloadCheckedLinks({
filename: "myvid.mp4",
url: $(this).data('vid')
});
$('#testmess').text("Downloading...");
});
}
function onWindowLoad() {
eventListeners();
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {
file: "getPagesSource.js"
}, function () {
// If you try and inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.runtime.lastError) {
message.innerText = 'There was an error injecting script : \n' + chrome.runtime.lastError.message;
}
});
}
window.onload = onWindowLoad;