Skip to content

Instantly share code, notes, and snippets.

@asl97
Last active January 13, 2019 00:30
Show Gist options
  • Save asl97/adf2d61c0ee3dbf7d827104f578a8696 to your computer and use it in GitHub Desktop.
Save asl97/adf2d61c0ee3dbf7d827104f578a8696 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name championscans download button
// @version 0.3
// @description Add a javascript powered download button
// @match https://reader.championscans.com/read/*
// @require https://stuk.github.io/jszip/dist/jszip.js
// @require https://stuk.github.io/jszip-utils/dist/jszip-utils.js
// @require https://stuk.github.io/jszip/vendor/FileSaver.js
// @grant none
// ==/UserScript==
// This here is the userscript version, tested on tampermonkey version 2.9.1 (Super old version which is the last open sourced version)
// This is some boilerplate stuff from the example https://stuk.github.io/jszip/documentation/examples/downloader.html
var Promise = window.Promise;
if (!Promise) {
Promise = JSZip.external.Promise;
}
/**
* Fetch the content and return the associated promise.
* @param {String} url the url of the content to fetch.
* @return {Promise} the promise containing the data.
*/
function urlToPromise(url) {
return new Promise(function(resolve, reject) {
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
// Create the button. it has the same content as the original so it would look more or less the same.
DL_button = document.createElement('div');
DL_button.innerHTML = '<a id="CS-DL-BUN" class="br-tab" title="Download""><i aria-hidden="true" class="br-i-download"></i></a>';
DL_button = DL_button.firstChild;
DL_downloading = false;
// The download function
DL_button_func = function(){
fetch(window.location.href+'/manifest.json').then(
(res) => {return res.json()}
).then(
(json) => {
var zip = new JSZip();
// Find every page
// Reason for using an element will be explain in DL_button_inject
for (img of json.readingOrder) {
zip.file(img.href.split('/').pop(), urlToPromise(img.href), {binary:true});
}
// When everything has been downloaded, we can trigger the dl
zip.generateAsync({type:"blob"}, function updateCallback(metadata) {
var msg = "progression : " + metadata.percent.toFixed(2) + " %";
console.log(msg);
}).then(function callback(blob) {
filename = "[Champion_Scans]"+json.metadata.belongsTo.series.name.replace(' ','_')+"_c"+json.metadata.belongsTo.series.position.padStart(2,0)+".zip";
// see FileSaver.js
saveAs(blob, filename);
console.log("done !");
DL_downloading = false;
}, function (e) {
alert(e);
DL_downloading = false;
});
}
)
return false;
}
DL_button_prefunc = function(){
// Prevent it from running again when it's still running
if (DL_downloading === true){
alert('Already downloading!')
return
}
DL_downloading = true;
DL_button_func();
}
DL_button.addEventListener( "click", DL_button_prefunc );
DL_button_exist_check = function(){
if(!document.getElementById("CS-DL-BUN")){
setTimeout(()=>{document.getElementsByClassName('br-cmenu')[0].getElementsByTagName('nav')[0].prepend(DL_button)}, 1000);
};
};
setInterval(DL_button_exist_check, 250);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment