-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDUX-file-helper.user.js
183 lines (154 loc) · 6.77 KB
/
EDUX-file-helper.user.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// ==UserScript==
// @name EDUX file helper
// @namespace http://use.i.E.your.homepage/
// @version 0.2
// @description enter something useful
// @grant GM_addStyle
// @grant GM_getResourceText
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js
// @require http://stuk.github.io/jszip/dist/jszip.js
// @require http://stuk.github.io/jszip-utils/dist/jszip-utils.js
// @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js
// @resource http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css
// @match https://edux.fit.cvut.cz/courses/*
// @copyright 2014+, flex
// ==/UserScript==
'use strict';
var newCSS = GM_getResourceText ("jqueryui_css");
GM_addStyle(newCSS);
this.$ = this.jQuery = jQuery.noConflict(true);
$(function(){
document.body.appendChild(document.createElement('div')).id = "download-dialog";
$( ".funding" ).before("<h2>File exports</h2><ul><li><a href='#' id='file_export_all' class='wikilink1'>Download all files</a></ul>");
var path = window.location.pathname.split( '/' );
var course_code = path[2];
// $( "#download-dialog" ).dialog({
// autoOpen: false,
// height: 300,
// width: 350,
// modal: true,
// buttons: {
// "Create an account": function() {
// var bValid = true;
// allFields.removeClass( "ui-state-error" );
// bValid = bValid && checkLength( name, "username", 3, 16 );
// bValid = bValid && checkLength( email, "email", 6, 80 );
// bValid = bValid && checkLength( password, "password", 5, 16 );
// bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
// // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
// bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. [email protected]" );
// bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
// if ( bValid ) {
// $( "#users tbody" ).append( "<tr>" +
// "<td>" + name.val() + "</td>" +
// "<td>" + email.val() + "</td>" +
// "<td>" + password.val() + "</td>" +
// "</tr>" );
// $( this ).dialog( "close" );
// }
// },
// Cancel: function() {
// $( this ).dialog( "close" );
// }
// },
// close: function() {
// allFields.val( "" ).removeClass( "ui-state-error" );
// }
// });
var course_files = [];
var download_files = function(files_to_download, zip, batch, batch_cnt) {
console.log("Running batch " + batch + ". File count "+batch_cnt);
var file_cnt = files_to_download.length;
var batch_size = 30;
if(file_cnt <= 0 || batch_cnt >= batch_size) {
var content = zip.generate({type:"blob"});
// see FileSaver.js
saveAs(content, course_code+"_export_"+batch+".zip");
if(file_cnt <= 0) return;
else {
batch += 1;
batch_cnt = 0;
zip = new JSZip();
}
}
var page_size = 10;
var files = files_to_download.slice(0, page_size);
var rest = files_to_download.slice(page_size);
var requests = files.map(function(file) {
var url = "https://edux.fit.cvut.cz" + file;
return $.ajax(url, {
dataType: "text",
beforeSend: function( xhr, settings ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined");
xhr.url = settings.url;
},
success: function(data, status, xhr) {
var url = xhr.url;
var url_parts = url.split("/");
var file_name = url_parts[url_parts.length - 1];
//zip.file("Hello.txt", "Hello World\n");
zip.file(file_name, data, {binary: true});
}
});
});
$.when.apply(null, requests).done(function() {
download_files(rest, zip, batch, batch_cnt + page_size);
});
};
var download_all_zip = function(files_to_download) {
console.log("Got " + course_files.length + " files.")
var zip = new JSZip();
var files = files_to_download;//.splice(0, 30);
download_files(files, zip, 1, 0);
};
var get_child_namespaces = function(doc, parent_namespace, current_depth) {
var child_depth = current_depth + 1;
var ns_nodes = doc.querySelectorAll(".media.level" + child_depth + ".closed");
return [].map.call(ns_nodes, function(node) {
var link = node.querySelector("a.idx_dir");
var prefix = parent_namespace;
if(prefix != "")
prefix += "/";
return prefix + link.text;
});
};
var process_files = function(file_nodes, namespace) {
// console.log("Files for "+namespace+":");
// console.log(file_nodes);
//file_nodes.querySelector("");
[].forEach.call(file_nodes, function(file_node) {
var link = file_node.querySelector("a:nth-child(3)");
var href = link.getAttribute('href');
course_files.push(href);
});
};
var generate_doc = function(data) {
var doc = document.implementation.createHTMLDocument();
//Replace scripts
data = data.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
doc.documentElement.innerHTML = data;
return doc;
}
var fetch_ns_info = function(course_code, namespace, depth) {
if(namespace == "av" || namespace == "student") return;
var success = function( data ) {
var doc = generate_doc(data);
var file_nodes = doc.querySelectorAll('#media__content div.odd, #media__content div.even');
process_files(file_nodes, namespace);
var child_namespaces = get_child_namespaces(doc, namespace, depth);
child_namespaces.forEach(function(ns) {
//console.log(ns);
fetch_ns_info(course_code, ns, depth + 1);
});
};
var url = 'https://edux.fit.cvut.cz/courses/'+ course_code + '/lib/exe/mediamanager.php?ns='+namespace;
$.get(url, success);
};
var namespace = "lectures";
fetch_ns_info(course_code, "", 0);
$( "#file_export_all").click(function(event){
event.preventDefault();
download_all_zip(course_files);
});
});