-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
79 lines (71 loc) · 2.99 KB
/
main.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
// ==UserScript==
// @name Docenti unina autodownloader button (with popups!)
// @namespace https://puntodegenere.netlify.app/
// @version 1.1
// @description Creates a button that allows you to download all files at once.
// @author Frollo & ZOLV
// @match https://www.docenti.unina.it/
// @icon https://www.google.com/s2/favicons?sz=64&domain=unina.it
// @grant none
// ==/UserScript==
(function() {
'use strict';
let base_url_string = 'https://www.docenti.unina.it/webdocenti-be/allegati/materiale-didattico/';
let pdfPattern = /\.pdf$/; // ending with .pdf
let filePattern = /\..+$/; // ending with .* (to exclude directories)
let separator = '__';
function createButton(context, func) {
var button = document.createElement("input");
button.type = "button";
button.value = "Download all files";
button.onclick = func;
context.appendChild(button);
}
window.onload = function() {
createButton(document.body, function() {
let angular_element = document.querySelector('[ng-show*="materialeDidatticoController"]');
let files = angular.element(angular_element).controller().getMaterialeDidattico().contenutoCartella;
let prof_id = angular.element(angular_element).controller().getIdProfessor();
window.base_directory_url_string = 'https://www.docenti.unina.it/webdocenti-be/docenti/' + prof_id + '/materiale-didattico/areapubb/';
for (let file of files) {
download_element(file, '');
}
}
);
};
function download_element(file, name_prepend) {
let filename = base_url_string + String(file.id);
//console.log(file);
if (pdfPattern.test(file.nome)) {
fetch(filename, { // codice rubato
method: 'GET'
}).then(resp => resp.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = name_prepend + file.nome; // the filename you want
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
}
else if (filePattern.test(file.nome)){
window.open(filename);
}
else { // This is supposed to only be directories
fetch(window.base_directory_url_string + String(file.id) + '?codIns=', {
method: 'GET'
}).then(resp => resp.json())
.then(json => {
for (let element of json.contenutoCartella){
let prep = file.nome + separator;
if (name_prepend != '')
prep = name_prepend + separator + prep;
download_element(element, prep);
}
})
}
}
})();