Last active
August 29, 2015 14:14
-
-
Save fanzeyi/c46ab3dc5319ac24b7f0 to your computer and use it in GitHub Desktop.
Code Check Exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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