Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code Check Exporter
// ==UserScript==
// @name Code Check Exporter
// @namespace https://zr.is/
// @version 0.5
// @description Export all files in code checker. (SJSU)
// @author Zeyi Fan (Zeray Rice)
// @match http://cs20.cs.sjsu.edu:8080/codecheck/*
// @grant none
// @require https://raw.githubusercontent.com/Stuk/jszip/v2.4.0/dist/jszip.min.js
// @updateURL https://gist.github.com/fanzeyi/c46ab3dc5319ac24b7f0/raw/code-check-exporter.user.js
// ==/UserScript==
(function() {
var generateZip = function() {
var zip = new JSZip(),
preBlocks = [].slice.call(document.getElementsByTagName("pre")),
textareaBlocks = [].slice.call(document.getElementsByTagName("textarea")),
codeBlocks = preBlocks.concat(textareaBlocks);
codeBlocks.forEach(function(block) {
var content;
if(block.tagName === "TEXTAREA") {
content = block.value;
} else {
content = block.textContent;
}
zip.file(block.previousElementSibling.textContent, content);
});
var blob = zip.generate({
type: 'blob'
});
return blob;
};
var blobUrl = URL.createObjectURL(generateZip());
var downloadBtn = document.createElement('a');
downloadBtn.href = blobUrl;
downloadBtn.download = 'assignment.zip';
downloadBtn.innerHTML = 'Export';
document.body.insertBefore(downloadBtn, document.body.firstChild);
// Drag'n'Drop
document.addEventListener('dragover', function(e) {
e.preventDefault();
return false;
});
document.addEventListener('dragenter', function(e) {
e.preventDefault();
return false;
});
document.addEventListener('drop', function(e) {
var dt = e.dataTransfer,
files = [].slice.call(dt.files),
textareaBlocks = [].slice.call(document.getElementsByTagName("textarea"));
e.preventDefault();
files.forEach(function(file) {
if(file.name.slice(-5) != ".java") {
return false;
}
textareaBlocks.forEach(function(submitBlock) {
if(submitBlock.previousElementSibling.innerText === file.name) {
var reader = new FileReader();
reader.addEventListener('loadend', function(e, f) {
submitBlock.innerText = this.result;
});
reader.readAsText(file);
}
});
});
return false;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.