Skip to content

Instantly share code, notes, and snippets.

@delbertbeta
Created November 11, 2018 15:47
Show Gist options
  • Save delbertbeta/37125e12b2fddad073b722a9f497a594 to your computer and use it in GitHub Desktop.
Save delbertbeta/37125e12b2fddad073b722a9f497a594 to your computer and use it in GitHub Desktop.
A batch for downloading all thing on SCUT e-online.
const jszipDom = document.createElement("script");
jszipDom.setAttribute("src", "https://cdn.bootcss.com/jszip/3.1.5/jszip.js");
const jszipUtilsDom = document.createElement("script");
jszipUtilsDom.setAttribute("src", "https://cdn.bootcss.com/jszip-utils/0.0.2/jszip-utils.min.js");
const fileSaverDom = document.createElement("script");
fileSaverDom.setAttribute("src", "https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.js");
document.body.appendChild(jszipDom);
document.body.appendChild(jszipUtilsDom);
document.body.appendChild(fileSaverDom);
const listFrame = document.getElementById("mainFrame").contentDocument.getElementsByTagName("frame")[1];
console.log("Start after 5s...");
setTimeout(function () {
console.info("Downloading...")
const generate = function () {
zip.generateAsync({
type: "blob"
}, function updateCallback(metadata) {
let msg = "Packing...Progression : " + metadata.percent.toFixed(2) + " %";
if (metadata.currentFile) {
msg += ", current file = " + metadata.currentFile;
}
console.log(msg);
})
.then(function callback(blob) {
saveAs(blob, "zipped.zip");
console.info("Done!");
}, function (e) {
console.error(e);
});
}
let urlList = [];
const zip = new JSZip();
Array.from(listFrame.contentDocument.getElementsByClassName("common indentten")).map(
e => {
return {
url: e.children[1].href.replace(/preview\/download_preview/, "download"),
name: e.children[1].textContent.trim()
}
}).filter(e => {
return !e.url.includes("folderid=");
}).forEach((e, i, a) => {
console.log(e.name + ": " + e.url);
urlList.push(e);
});
let count = 0, total = urlList.length;
(function downloadList() {
if (urlList.length > 0) {
let item = urlList.shift();
fetch(item.url, {
credentials: "include"
}).then(response => {
response.blob().then(blob => {
let extension = response.headers.get("content-disposition").match("\"(.*)\"")[1].split(".").pop();
item.name += "." + extension;
console.log("Downloaded(" + ++count + "/" + total + "): " + item.name);
zip.file(item.name, blob, {
binary: true
});
downloadList();
}, e => {
console.error(e);
})
}, e => {
console.error(e);
})
} else {
generate();
}
})()
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment