-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
126 lines (105 loc) · 3.74 KB
/
script.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
122
123
124
125
126
$(document).on("change", ".file-container #file", function() {
let input_encoded, input_decoded, reader;
let prefix = "https://web.microsoftstream.com";
let output_element = $(".output-container .linklist"); // list of links
let summary_element = $(".output-container .summary"); // how many links have been found
let download_file = $(".file-container a#filedownload") // download link
let bar = $(".progressbar") // loading bar
let filename;
reader = new FileReader();
reader.onload = function() {
// clean urls containers in page
cleanUrls(output_element, download_file, summary_element);
$(bar).css({"display": "block"});
$(bar).children(":first").css({"width": "0"});
$(bar).children(":first").animate({"width": "100%", "display": "inline"}, 700, function() {
$(bar).css({"display": "none"});
// extract the base64 encoded content of the file
input_encoded = reader.result;
// decode from base64 and skip the first part, comma separated, containing infos about the file
input_decoded = atob(input_encoded.split(",")[1]);
// extract urls
urls = extractUrls(input_decoded, prefix);
// show urls on container
showUrls(urls, output_element, summary_element);
if (urls.length > 0) {
// if we found any urls, format all the links
attachDownload(urls, download_file, filename);
}
})
};
// extract file from file container
reader.readAsDataURL($(this).prop('files')[0]);
// extract the filename to be used later
filename = $(this).prop('files')[0].name.split(".")[0];
});
function cleanUrls(element, download, summary) {
// clean output containers from link
// link list
$(element).text("");
// p containing the number of found links
$(summary).text("Ricerca in corso...");
// download button
$(download).attr({
"href": "",
"download": "",
"active": "false"
});
// download label
$(download).children().attr({
"active": "false"
});
}
function extractUrls(text, prefix) {
// extracs urls from html text, regex thanks to https://github.com/valerionew
let re = /\/video\/.{36}/g;
let urls = text.match(re);
if (urls === null) {
return [];
}
// we convert the list into a set (and then back) to remove duplicates
let urls_set = new Set(urls);
let unique_urls = [];
urls_set.forEach(function(item) {
// while reconverting, we add the prefix (the base url)
unique_urls.push(prefix + item);
});
return unique_urls;
}
function showUrls(urls, element, summary) {
// shows url list in output
if (urls.length > 0) {
if (urls.length > 1) {
$(summary).text(`Sono stati trovati ${urls.length} link nel file:`);
} else {
$(summary).text(`È stato trovato 1 link nel file:`);
}
urls.forEach(function(item) {
$(element).append(`<span id="link"><a href="${item}">${item}</a></span><br>`);
});
} else {
$(summary).text('Non sono stati trovati link nel file!');
}
}
function attachDownload(urls, element, filename) {
// create downloadable file
let prefix = "data:text/plain;base64,";
let filetype = ".txt";
let output = "";
let output_encoded;
urls.forEach(function(item) {
output += item + '\r\n';
});
// base64 encoding because firefox doesn't like plaintext
output_encoded = btoa(output);
// download button
$(element).attr({
"href": prefix + output_encoded,
"download": filename + filetype,
"active": "true"
});
// download label
$(element).children().attr({
"active": "true"
});
}